2011-11-11 191 views
1

我得到了一個自定義的類,它從xml擴展了它的佈局。在那個XML裏我有一個按鈕。 然後在我的活動我instanziate該自定義類並將其添加到:從按鈕點擊獲取父對象

  1. 線性佈局(自定義類的視圖)
  2. 一個類型數組(孔對象)

現在我希望如果按下按鈕,對象將從兩個類型的數組和佈局中移除。 現在我的問題是,首先我得到兩個地方,我必須刪除對象,第二,我不能找到一種方法來查找類型的數組中的對象。該按鈕僅返回其視圖。使用.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中被刪除。

回答

0

這是我的第二個答案,在您的問題中添加更多詳細信息後編寫。第一個仍然有效,但並不適用於你的情況。我的建議是將你的按鈕的代碼放在原始活動中,通過使用已經作爲回調傳遞的按鈕。喜歡的東西:

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因爲它在一個匿名類中使用)。如果您需要能夠建立你MCustomObject從多個活動(不僅MActivity,那麼我建議你解壓出來的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; 
} 

(注:用

Map<String, MCustomObject> 

中,你customIds映射爲對象將麥免去您的列表objList e查找更容易閱讀)

+0

對我有用,謝謝大家的支持! – masi

+0

不客氣,這對我也很有趣:) – Guillaume

0

您可以使用

View.getId() 

找到你的數組中的按鈕。

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

我不認爲按鈕在數組中,他正在談論自定義視圖。但我同意從數組中獲取某些東西的方法。 – Guillaume

+0

好吧,我正在談論的自定義對象是確切的。我不知道它的觀點。它是一個簡單的java類,它從xml擴展了它的佈局。這是否使它成爲一個觀點? – masi

+0

它應該從'View'或繼承自'View'類的其他東西繼承。 – Caner

0

我LAS_VEGAS用戶, 同意當您創建按鈕,您應該設置按鈕ID(button.id =),你可以通過

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

    } 
} 
+0

我不想要按鈕被刪除,但包含按鈕的孔自定義類。 – masi

0

指出他們這是我會怎麼做。我建議使用一個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 
} 
+0

以及我不知道這是否有幫助。就像我說過的那樣,自定義對象基本上是一個簡單的java類的實例,它本身使用膨脹方法從xml獲取佈局。這是否讓孔類成爲一個視圖?或者我必須搜索類型?如果是這樣,我如何獲得對類型的引用而不是對其視圖的引用? – masi

+0

我認爲你的自定義對象是擴展視圖,正如你所說「它從XML擴展其佈局」。那麼,如果它是一個自定義對象,你需要一些方法來識別它,爲什麼不有一個ID字段呢?我已經編輯我的示例,將其重命名爲Custom(而不是View),但這個想法是一樣的... – Guillaume

+0

另外,如果您在您的問題中添加了一些代碼,獲得具體幫助將會更容易 – Guillaume