通过单击按钮获取父对象
我有一个自定义类,它从 xml 扩展其布局。在该 xml 中我有一个按钮。 然后在我的活动中,我实例化该自定义类并将其添加到:
- 线性布局(自定义类的视图)
- 类型化数组(孔对象)
现在我希望如果按下按钮,对象将从两者中删除,类型化数组和布局。 现在我的问题是,首先我有两个地方必须删除对象,其次我找不到在类型化数组中“查找”对象的方法。该按钮仅返回其视图。使用 .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:
- a linear layout (the view of the custom class)
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是您在问题中添加更多详细信息后写的第二个答案。第一个仍然有效,但并不真正适用于您的情况。我的建议是将按钮的代码放在原始活动中,使用您已经作为回调传递的代码。类似于:
我将上下文参数类型更改为
MActivity
(并将其设为final
因为它在匿名类中使用)。如果您需要能够从多个活动(不仅仅是MActivity
)构建您的MCustomObject
,那么我建议您将removeCustomObject
方法提取到接口中,然后让所有活动都实现它。在您的
MActivity
中,您添加以下方法:(注意:将您的列表
objList
替换为您将 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:
I changed the context parameter type to
MActivity
(and made itfinal
because it's used in an anonymous class). If you need the possibility to build yourMCustomObject
from multiple activities (not onlyMActivity
, then I suggest you extract out theremoveCustomObject
method into an interface, and you make all the activities implement it.And in your
MActivity
, you add the following methods:(NB: replacing your list
objList
with ain which you map customIds to objects would make the lookup easier to read)
您可以使用它
来查找数组中的按钮。
You can use
to find the button in your array.
我就是这样做的。我建议使用 ArrayList 而不是简单的数组,因为它对删除等有更好的支持。
缺少一些上下文来为您提供完整的解决方案(如何决定要删除的
viewId
?),但这应该可以帮助您继续。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.我同意 LAS_VEGAS 用户的观点,
当你创建按钮时,你应该设置按钮 id (button.id= ) 并且你可以通过
I agree with LAS_VEGAS user,
when you create buttons you should set button id (button.id= ) and you can point them by