1
votes

I have created a custom ListView with radioButton on each its rows using Custom adapter. I want to programmatically check a radio button when its parent view (list item) is checked, and then uncheck other RadioButtons inside other ListView items. Anyone can provide me the solutions?

UPDATE

Here is my getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout v = null;

    final Item i = items.get(position);

    EntryItem ei = (EntryItem) i;
    v = (LinearLayout) vi.inflate(R.layout.list_item_entry, null);
    final TextView title = (TextView) v
                    .findViewById(R.id.list_item_entry_title);
    final TextView subtitle = (TextView) v
                    .findViewById(R.id.list_item_entry_summary);


    RadioButton radioButton = new RadioButton(context);
    radioButton.setFocusable(false);
    radioButton.setFocusableInTouchMode(false);
    v.addView(radioButton); 

    if (title != null)
                title.setText(ei.title);
    if (subtitle != null)
                subtitle.setText(ei.subtitle);


    convertView = v;
    return convertView;
}
4

4 Answers

4
votes

You can use An int variable if a single item should be selected, initialize with -1;

int index =-1;

in getView method of add a line:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout v = null;

    final Item i = items.get(position);

    EntryItem ei = (EntryItem) i;
    v = (LinearLayout) vi.inflate(R.layout.list_item_entry, null);
    final TextView title = (TextView) v
                    .findViewById(R.id.list_item_entry_title);
    final TextView subtitle = (TextView) v
                    .findViewById(R.id.list_item_entry_summary);


    RadioButton radioButton = new RadioButton(context);
    radioButton.setFocusable(false);
    radioButton.setFocusableInTouchMode(false);
    radioButton.setChecked(index==position);
    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                       
                    @Override
                    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

                    }
                });
                v.addView(radioButton);
            } 
            if (title != null)
                title.setText(ei.title);
            if (subtitle != null)
                subtitle.setText(ei.subtitle);
        }

    convertView = v;
    return convertView;
}

and in OnItemClick method of listener, do:

index=position;
adapter.notifyDataSetInvalidated();
2
votes

To reset the radio buttons, do it in getView method..

in the ListView.onsetOnItemClickListener():

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        //get the radio button and check it
                    view.findViewById(); ....   
        }
    });
1
votes
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

Above code is from android.R.layout.simple_spinner_dropdown_item

It does exactly what you want to achieve. May be this can be helpful to you.

1
votes

You can manage it in the click event of list view...in setOnClickListener event, you can get the Radiobutton though view..and then you can manage it