Reputation: 1540
As simple as this seems I have not been able to find an easy way solution to this, I have a simple radio button list view and I want to extract the value of the radio button selected on button click event.
Here is the code you might have seen in a million other places.
public class RadioListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>this,android.R.layout.simple_list_item_single_choice, CONTENT));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private static final String[] CONTENT = new String[] {"bla1","bla2","bla3","bla4","bla5"};
}
ANY help is appreciated! :]
Upvotes: 1
Views: 3635
Reputation: 1540
The answer lies in two simple lines of codes which I had to scavenge for days at end.
Just adding the below two lines to the onClick event will extract the index of the item selected along with the value at it!
ListView l=getListView();
Object obj=l.getItemAtPosition(l.getCheckedItemPosition());
Hope someone finds the above helpful. Cheers!
Upvotes: 2
Reputation: 132992
extract the value of the radio button selected on button click event.
you can get selected value from ListView using ListView.getSelectedItemPosition
as on Button click:
@Override
public void onClick(View v)
{
int selectedindex=RadioListActivity.this.
getListView().getSelectedItemPosition();
String str_selectedtxt=CONTENT[selectedindex];
}
Upvotes: 1