1

I have ListAdapter which fills list with TextView of category and RadioButton which is to be checked. So, the problem is that I want user to define which category he wants through List and when he clicks on RadioButton, others should be unchecked and so on. I need to set somehow the ID of this RadioButtons and I'm doing that in getView(...) method in ListAdapter but when it calls for second time that method, he doesn't find that RadioButton and I get error while trying to set ID of this RadioButton view. I was debugging it, and first time it finds the view through method findViewById, but second time it doesn't.

I think I might know the problem for it -> when second time it goes to find the view by ID, it can't find it since I've put already other Identifier on the first call, but how can I set unique identifiers for my RadioButtons in the list then?

Here is my code: 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;
}

and here is my layout for 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"/>

So, how to set unique identifier to my RadioButtons in view properly or can I make somehow group of RadioButtons which will care for itself that when other one is checked, others to be unchecked - something how in HTML is solved up.

Tommz
  • 3,393
  • 7
  • 32
  • 44
  • 2
    Look at http://stackoverflow.com/questions/7329856/how-to-use-radiogroup-in-listview-custom-adapter – trebron Aug 27 '13 at 14:01
  • Thanks, that helped, also this post: http://developmentality.wordpress.com/2010/11/05/of-rubber-stamps-and-checkboxes-why-your-listview-is-broken/ – Tommz Aug 29 '13 at 09:23

1 Answers1

0

By using setId(), you are changing the identifier associated with the RadioButton that is used to find it in the view tree. You should probably be using setTag() instead, which is documented here. This method allows you to attach some opaque data object to the view that can be retrieved later using the getTag() method.

Eugene S
  • 3,092
  • 18
  • 34