I had a similar situation but fixed that easily. Try this:
-
Extend ListActivity and set your custom list adapter
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); //if you need title to be hidden setContentView(R.layout.activity_add_to_cart_select_service_plan); setListAdapter(adapter); }
-
Create a field to store selected index position in adapter
int selectedIndex = -1;
-
Provide an interface to it
public void setSelectedIndex(int index){ selectedIndex = index; }
-
Set the checked state to true or false based on the selected index inside getView()
@Override public View getView(int position, View convertView, ViewGroup parent) { RadioButton rbSelect = (RadioButton) convertView .findViewById(R.id.radio1); if(selectedIndex == position){ rbSelect.setChecked(true); } else{ rbSelect.setChecked(false); } }
-
Set radio button focusable and clickable attribs to ‘false’
<RadioButton android:id="@+id/radio1" android:checked="false" android:focusable="false" android:clickable="false" />
-
Set listview descendantFocusability attrib to ‘beforeDescendants’
<ListView android:id="@android:id/list" android:choiceMode="singleChoice" android:descendantFocusability="beforeDescendants" />
-
Override onListItemClick
@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); adapter.setSelectedIndex(position); adapter.notifyDataSetChanged(); }
That’s it..run and check