我得到了一個自定義的類,它從xml擴展了它的佈局。在那個XML裏我有一個按鈕。 然後在我的活動我instanziate該自定義類並將其添加到:從按鈕點擊獲取父對象
- 線性佈局(自定義類的視圖)
- 一個類型數組(孔對象)
現在我希望如果按下按鈕,對象將從兩個類型的數組和佈局中移除。 現在我的問題是,首先我得到兩個地方,我必須刪除對象,第二,我不能找到一種方法來查找類型的數組中的對象。該按鈕僅返回其視圖。使用.parent.parent,直到我到達自定義類視圖的視圖,我可以從佈局中刪除它,但似乎沒有辦法從buttonpress獲取對象本身的引用。
也許洞的概念我怎麼做這是錯的,不知道。希望你能幫助。
編輯:到clearify升技
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中被刪除。
對我有用,謝謝大家的支持! – masi
不客氣,這對我也很有趣:) – Guillaume