如何在放射群中获取3个无线电频率?

时间:2013-11-11 07:38:31

标签: android listview radio-button listadapter radio-group

我如何调整此example与3 Radiobuttons我想在我的listadapter中获取所有选定的RadioButtons

2 个答案:

答案 0 :(得分:0)

像这样创建一个广播组。

 <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>

将此布局扩展到自定义适配器

使用setOnCheckedChangeListener从列表中获取所选值。

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);
            }
    }
});

有关详细信息,请参阅此link

答案 1 :(得分:0)

布局文件应为:

<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应该是:

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包含所选单选按钮的列表。