2013-08-27 5 views
1

카테고리의 TextView와 체크리스트에 RadioButton으로 채우는 ListAdapter이 있습니다. 그래서 문제는 사용자가 목록을 통해 원하는 범주를 정의하고 그가 RadioButton을 클릭하면 다른 항목을 선택하지 말아야한다는 것입니다. 어떻게 든이 RadioButtons의 ID를 설정해야하고 메서드에서 ListAdapter을 호출하지만 두 번째로 메서드를 호출하면 그는 RadioButton을 찾지 못하고 ID를 설정하는 동안 오류가 발생합니다. 이 RadioButton 뷰. 디버깅 중이었고 처음으로 메소드 findViewById을 통해 뷰를 찾았지만 두 번째로는 그렇지 않습니다.리스트 어댑터가 보이지 않음

내가 처음으로 다른 식별자를 넣었으므로 두 번째로 ID로보기를 찾으면 찾지 못할 수도 있지만 어떻게해야합니까? 내 RadioButton에 대한 고유 식별자를 목록에 설정합니까? 의 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를 고유 식별자를 설정하는 보기에 적절하게 또는 어떻게 든 다른 그룹이있을 때 스스로를 돌볼 RadioButton 그룹을 만들 수 있습니까? 체크 된 것들, 체크되지 않은 것들 - HTML에서의 해결 방법.

+2

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

+0

고마워요,이 게시물도 도움이되었습니다 : http : //developmentality.wordpress .com/2010/11/05/of-rubber-stamps-and-checkboxes-why-your-listview-is broken / – Tommz

답변

0

setId()을 사용하면 뷰 트리에서 찾을 때 사용되는 RadioButton과 관련된 식별자가 변경됩니다. 대신 here으로 기록 된 setTag()을 사용해야합니다. 이 메소드를 사용하면 나중에 불투명 한 데이터 오브젝트를 뷰에 첨부 할 수 있습니다.이 오브젝트는 나중에 getTag() 메소드를 사용하여 검색 할 수 있습니다.