2011-09-14 42 views
1

我標記了要刪除的另一個線程,因爲主要問題被編輯了很多次,以致它變得混亂。如何獲取setOnCheckedChangeListener(ListView複選框工作)

所以問題是:我想用複選框填充一個ListView,因爲我想自定義我的ListView我沒有使用simple_multiple_choice_mode,我把我的佈局放在list_item.xml中:(對於每一行)

 <CheckBox> 
       xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm" 
       android:layout_width="fill_parent" 
        android:layout_height="wrap_removed" android:id="@+id/nomeAPP" style="?listItem"&gt; 
        </<CheckBox> 

我的ListView是lista.xml

<ListView android:id="@android:id/list" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    style="?whiteBackground"> 

我的適配器:

 list=getListView(); 
       this.setListAdapter(new ArrayAdapter&lt;String&gt;(this, 
         R.layout.list_item, aux)) 

所以我想爲複選框做一個setOnCheckedChangeListener,所以我可以將選擇的項存儲在一個數組中。

所以我做了這個監聽器:

 CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01); 
       cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         // TODO Auto-generated method stub 
         if (buttonView.isChecked()) { 
          Toast.makeText(getBaseContext(), &quot;Checked&quot;, 
            Toast.LENGTH_SHORT).show(); 
          } 
         else 
         { 
          Toast.makeText(getBaseContext(), &quot;UnChecked&quot;, 
            Toast.LENGTH_SHORT).show(); 
         } 

        } 
        }); 

問題是:我得到的NULL例外。該好奇的事情是:如果我一個簡單的

<ScrollView android:id="@+id/widget54" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <CheckBox android:text="Checkbox" android:id="@+id/CheckBox01" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    </LinearLayout> 

</ScrollView> 

「開關」我的佈局,而忘記我的ListView(和我的轉接器),它的工作原理!那麼,我的佈局有什麼問題?

我創建了一個新的小項目,只是爲了讓大家看清楚情況。它可用HERE現在不起作用,但如果您取消註釋適配器,將ListActivity更改爲活動並更改setContentView,它將起作用(選中/取消選中Toast文本)。

我發現另一個奇怪的是,與ListActivity如果它的相關不從未工作....不知道...

+1

你能發佈null異常日誌嗎? –

回答

1

好吧,我想我得到了你的問題,

CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01); 

該作品在第二個佈局中,因爲您可能已經使用setcontentview()爲該活動設置了視圖。

所以,在你列表活動的問題是,上述行不知道,哪種佈局它尋找找到ID,

R.id.CheckBox01 

,你必須可以訪問佈局查看你正在充氣的列表視圖,我們稱之爲inflatedView,然後你像這樣訪問複選框。

CheckBox cb = (CheckBox) inflatedView.findViewById(R.id.CheckBox01); 

如果您沒有設置內容視圖,則必須告訴函數findViewById(),它必須查找視圖。 我會證實這一點,如果你發佈NPE日誌。

HTH。

+0

不幸的是,在你的想法中有一些缺失 - 是,setContentView(R.Layout。Lista)用於「第一種情況」,因此該應用程序知道ListView和適配器在哪裏知道每行的R.layout.List_item ....即使如此,我嘗試使用您的解決方案,米不知道如何適應。該行之前我應該​​怎麼做?因爲inflatedView是一個東西的變量...一個視圖? @yashwanthkumar你有所有的代碼可用,如果你想有一個相貌.. – Tiago

+0

遵循2個鏈接,我認爲他們會幫助你正確訪問你的複選框,想法是,你必須訪問複選框在getView()方法,http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html,http://www.ezzylearning.com/tutorial.aspx?tid= 1763429&q = customizing-android-listview-items-with-custom-arrayadapter –

+0

謝謝。我現在在定製適配器時遇到了一些問題,但我確定這是正確的。 – Tiago