How I can adapt this example with 3 Radiobuttons
i want to fetch all the selected RadioButtons
in my listadapter.
Asked
Active
Viewed 181 times
0
-
you can iterate your listadapter and check the radiobutton isChecked value. Let me know if this works. – Aakash Nov 11 '13 at 07:49
-
how i can do this plz – amine Nov 20 '13 at 09:27
2 Answers
0
Create a radio group like this.
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioChild1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioChild1"
android:checked="true" />
<RadioButton
android:id="@+id/radioChild2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioChild2" />
<RadioButton
android:id="@+id/radioChild3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioChild3" />
</RadioGroup>
inflate this layout to your custom adapter
Use setOnCheckedChangeListener
to get the selected values from the list.
ArrayList<String> selectedCheckBox = new ArrayList<String>();
YourView.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
selectedCheckBox.add(values);
}
else
{
selectedCheckBox.remove(values);
}
}
});
For more details refer this link

Community
- 1
- 1

Rethinavel
- 3,912
- 7
- 28
- 49
-
-
-
is not work !! i have listadapter(size=10) with 3 radio for every column – amine Nov 20 '13 at 08:14
0
Layout file should be :
<RadioGroup
android:id="@+id/grp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="op1" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="op2" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="op3" />
</RadioGroup>
ArrayAdapter should be :
private class ViewHolder {
protected TextView text;
protected RadioGroup radioGroup;
}
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
HashMap<Integer, RadioButton> mapRadioBtnsSelected;
ArrayList<View> arrViews;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
mapRadioBtnsSelected = new HashMap<Integer, RadioButton>();
arrViews = new ArrayList<View>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.grp);
viewHolder.radioGroup.setTag(position);
convertView.setTag(viewHolder);
arrViews.add(convertView);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getName());
viewHolder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
int getPosition = (Integer) group.getTag();
if(mapRadioBtnsSelected.containsKey(getPosition))
mapRadioBtnsSelected.remove(getPosition);
mapRadioBtnsSelected.put(getPosition, arrViews.get(getPosition).findViewById(checkedId);
}
}
});
return convertView;
}
}
public class Model {
private String name;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
mapRadioBtnsSelected contains the list of selected radio buttons.

Aakash
- 1,860
- 19
- 30