通过单击按钮获取父对象

发布于 2024-12-15 02:34:39 字数 1833 浏览 2 评论 0原文

我有一个自定义类,它从 xml 扩展其布局。在该 xml 中我有一个按钮。 然后在我的活动中,我实例化该自定义类并将其添加到:

  1. 线性布局(自定义类的视图)
  2. 类型化数组(孔对象)

现在我希望如果按下按钮,对象将从两者中删除,类型化数组和布局。 现在我的问题是,首先我有两个地方必须删除对象,其次我找不到在类型化数组中“查找”对象的方法。该按钮仅返回其视图。使用 .parent.parent 直到到达自定义类视图的视图为止,我可以将其从布局中删除,但似乎没有办法从按钮按下时获取对对象本身的引用。

也许我的做法是错误的,不知道。希望你能帮忙。

编辑:澄清一点

MActivity:

public class MActivity extends Activity{
private ArrayList<MCustomObject> objList = new ArrayList<MCustomObject>();
@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout;

    MCustomObject obj1 = new CustomObject(this, "blabla1");
    objList.add(obj1);
    MCustomObject obj2 = new CustomObject(this, "blabla2");
    objList.add(obj2);
    MCustomObject obj3 = new CustomObject(this, "blabla3");
    objList.add(obj3);
    }
}

MCustomObject:

public class MCustomObject{

public MCustomObject(Context context, String xyz){
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.m_custom_object_layout, null);

button = (Button) view.findViewById(R.id.mButton);

[...]

m_custom_object_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


        <Button
            android:id="@+id/mButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/delete" />
</LinearLayout>

现在当我按下mButton时,我希望按钮所属的孔obj实例从objList中删除。

i got a custom class which inflates its layout from a xml. within that xml i got a button.
then in my activity i instanziate that custom class and add it to:

  1. a linear layout (the view of the custom class)
  2. a typed array (the hole object)

now i want that if the button is pressed the object gets removed from both, the typed array and the layout.
now my problem is that first i got two places where i have to remove the object and second that i cant find a way to "find" the object in the typed array. the button only returns its view. using .parent.parent till i reach the view of the custom class' view i'm able to remove it from the layout but there dosn't seem to be a way to get a reference to the object itself from the buttonpress.

maybe the hole concept how i'm doing it is wrong, dunno. hope you can help.

EDIT: to clearify abit

MActivity:

public class MActivity extends Activity{
private ArrayList<MCustomObject> objList = new ArrayList<MCustomObject>();
@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout;

    MCustomObject obj1 = new CustomObject(this, "blabla1");
    objList.add(obj1);
    MCustomObject obj2 = new CustomObject(this, "blabla2");
    objList.add(obj2);
    MCustomObject obj3 = new CustomObject(this, "blabla3");
    objList.add(obj3);
    }
}

MCustomObject:

public class MCustomObject{

public MCustomObject(Context context, String xyz){
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.m_custom_object_layout, null);

button = (Button) view.findViewById(R.id.mButton);

[...]

m_custom_object_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


        <Button
            android:id="@+id/mButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/delete" />
</LinearLayout>

now when i press mButton i want that the hole obj instanz that the button belongs to gets deleted from the objList.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扬花落满肩 2024-12-22 02:34:39

这是您在问题中添加更多详细信息后写的第二个答案。第一个仍然有效,但并不真正适用于您的情况。我的建议是将按钮的代码放在原始活动中,使用您已经作为回调传递的代码。类似于:

public class MCustomObject{
    private final String customId;

    public MCustomObject(final MActivity parent, final String customId, String xyz){ 
        this.customId = customId;
        LayoutInflater layoutInflater = LayoutInflater.from(parent);
        view = layoutInflater.inflate(R.layout.m_custom_object_layout, null);
        button = (Button) view.findViewById(R.id.mButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // do your other stuff
               parent.removeCustomObject(customId);
            }
        });
    }

    public String getId() {
        return customId;
    }

我将上下文参数类型更改为 MActivity (并将其设为 final 因为它在匿名类中使用)。如果您需要能够从多个活动(不仅仅是 MActivity)构建您的 MCustomObject,那么我建议您将 removeCustomObject 方法提取到接口中,然后让所有活动都实现它。

在您的 MActivity 中,您添加以下方法:(

public void removeCustomObject(String customId) {
    objList.remove(findCustomObjectWithId(id));
}

private MCustomObject findCustomObjectWithId(int id) {
    for(MCustomObject custom : objList) {
        if (custom.getId() == id) {
            return custom;
        }
    }
    return null;
}

注意:将您的列表 objList 替换为

Map<String, MCustomObject>

您将 customIds 映射到的 列表)。对象将使查找更容易阅读)

This is my second answer written after you've added more details in your question. First one is still valid but doesn't really apply to your situation. What I suggest is put the code for your button in the original activity, by using the one you're already passing as a callback. Something like:

public class MCustomObject{
    private final String customId;

    public MCustomObject(final MActivity parent, final String customId, String xyz){ 
        this.customId = customId;
        LayoutInflater layoutInflater = LayoutInflater.from(parent);
        view = layoutInflater.inflate(R.layout.m_custom_object_layout, null);
        button = (Button) view.findViewById(R.id.mButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // do your other stuff
               parent.removeCustomObject(customId);
            }
        });
    }

    public String getId() {
        return customId;
    }

I changed the context parameter type to MActivity (and made it final because it's used in an anonymous class). If you need the possibility to build your MCustomObject from multiple activities (not only MActivity, then I suggest you extract out the removeCustomObject method into an interface, and you make all the activities implement it.

And in your MActivity, you add the following methods:

public void removeCustomObject(String customId) {
    objList.remove(findCustomObjectWithId(id));
}

private MCustomObject findCustomObjectWithId(int id) {
    for(MCustomObject custom : objList) {
        if (custom.getId() == id) {
            return custom;
        }
    }
    return null;
}

(NB: replacing your list objList with a

Map<String, MCustomObject>

in which you map customIds to objects would make the lookup easier to read)

七婞 2024-12-22 02:34:39

您可以使用它

View.getId()

来查找数组中的按钮。

for(int i =0; i < array.size(); i++) {
    if(button.getId() == array.get(i).getId())
    /* Found! */ 
}

You can use

View.getId()

to find the button in your array.

for(int i =0; i < array.size(); i++) {
    if(button.getId() == array.get(i).getId())
    /* Found! */ 
}
感情废物 2024-12-22 02:34:39

我就是这样做的。我建议使用 ArrayList 而不是简单的数组,因为它对删除等有更好的支持。
缺少一些上下文来为您提供完整的解决方案(如何决定要删除的 viewId ?),但这应该可以帮助您继续。

private List<Custom> myListOfCustom = new ArrayList<Custom>();
// initialize myListOfCustom the way you want by using myListOfCustom.add(object)

// button clicked
public void buttonClicked(View v) {
    final int id = ...; // obtain id here
    myListOfCustom.remove(findCustomObjectWithId(id));
    // also remove the view from your layout
}

private Custom findCustomObjectWithId(int id) {
    for(Custom custom : myListOfCustomObjects) {
        if (custom.getId() == id) {
            return custom;
        }
    }
    return null; // custom object with givenm id was not in the list! alternatively, you can throw a RuntimeException here (something like an IllegalArgumentException) and catch it in the buttonClicked method
}

This is how I would do it. I suggest using an ArrayList instead of a simple array, as it has better support for things like remove, etc.
Some context is missing to give you a full solution (how to decide with viewId you want to remove?), but that should get you going.

private List<Custom> myListOfCustom = new ArrayList<Custom>();
// initialize myListOfCustom the way you want by using myListOfCustom.add(object)

// button clicked
public void buttonClicked(View v) {
    final int id = ...; // obtain id here
    myListOfCustom.remove(findCustomObjectWithId(id));
    // also remove the view from your layout
}

private Custom findCustomObjectWithId(int id) {
    for(Custom custom : myListOfCustomObjects) {
        if (custom.getId() == id) {
            return custom;
        }
    }
    return null; // custom object with givenm id was not in the list! alternatively, you can throw a RuntimeException here (something like an IllegalArgumentException) and catch it in the buttonClicked method
}
听不够的曲调 2024-12-22 02:34:39

我同意 LAS_VEGAS 用户的观点,
当你创建按钮时,你应该设置按钮 id (button.id= ) 并且你可以通过

public void onClick(View v) 
{
    if(v.getId ==  )
    {
          //whatever you want
    }
    else
    {

    }
}

I agree with LAS_VEGAS user,
when you create buttons you should set button id (button.id= ) and you can point them by

public void onClick(View v) 
{
    if(v.getId ==  )
    {
          //whatever you want
    }
    else
    {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文