2013-08-27 81 views
1

我有ListAdapter它填寫列表TextView的類別和RadioButton這是要檢查。所以,問題是我希望用戶通過List定義他想要的類別,當他點擊RadioButton時,其他人應該被取消選中等等。我需要以某種方式設置此RadioButtons的ID,我在getView(...)方法ListAdapter中,但當它要求第二次該方法,他不會找到該RadioButton,我試圖設置ID這個RadioButton視圖。我正在調試它,第一次它通過方法findViewById找到視圖,但第二次沒有。ListAdapter沒有得到視圖

我想我可能知道它的問題 - >當第二次通過ID查找視圖時,它找不到它,因爲我已經在第一次調用時放置了其他標識符,但是我怎麼能在列表中爲我的RadioButtons設置唯一的標識符呢?

這裏是我的代碼: getView:

public View getView(int index, View view, ViewGroup parent) { 

    if (view == null){ 
     LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
     view = inflater.inflate(R.layout.category_item, parent, false); 
    } 

    ListCategoryItem category = categories.get(index); // categories is list 

    TextView title = (TextView) view.findViewById(R.id.categoryTitle); 
    naziv.setText(kategorija.getNazivKategorije()); 

    RadioButton radioBtn = (RadioButton) view.findViewById(R.id.categoryRadioButton); 
    radioBtn.setId(category.getIdCategory()); 

    return view; 
} 

,這裏是我的category_item佈局:

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


<TextView 
    android:id="@+id/categoryTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:paddingTop="5dp" 
    /> 

<RadioButton 
    android:id="@+id/categoryRadioButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:textSize="15sp" 
    android:onClick="onRadioButtonClick"/> 

那麼,如何唯一標識設置爲我的單選按鈕在正確的觀點或我可以讓某組RadioButtons將關心自己,當其他人檢查,其他人未經檢查 - 如何在HTML中解決了。

+2

看看http://stackoverflow.com/questions/7329856/how-to-use-radiogroup-in-listview-custom-adapter – trebron

+0

謝謝,這有幫助,也是這個職位: http://developmentality.wordpress .COM/2010/11/05 /的-橡皮圖章和 - 複選框 - 爲什麼 - 您 - 列表視圖 - 被破碎/ – Tommz

回答

0

通過使用setId(),您正在更改與用於在視圖樹中查找它的RadioButton關聯的標識符。您應該使用setTag()來代替,其記錄爲here。此方法允許您將一些不透明的數據對象附加到可以稍後使用getTag()方法檢索的視圖。