如何在自定义ListView适配器中处理RadioGroup的onCheckedChangeListener

时间:2012-02-22 09:52:15

标签: android android-listview radio-button android-arrayadapter custom-adapter

我正在开发一个具有自定义布局列表视图的应用程序,如下所示:
它有RadioButtons RadioGroupTextView。实际上,它将显示为问题TextView)和答案RadioButtons)列表。上面的视图在我的自定义适配器中膨胀,该适配器扩展了ArrayAdapter<Question>

问题在于,我应该如何在自定义RadioButtons中维护Adapter的状态?当RadioButtonpressed/checked且列表向下滚动时,adapter会自动回收视图,RadioButton状态将丢失。

  • 那么,任何人都可以指导有关此内容的任何链接/信息吗?
  • 或者我应该如何实施问题答案清单?

第一条遵循: http://www.vogella.de/articles/AndroidListView/article.html

以上链接使用的是CheckBox,我希望使用RadioGroupRadioButtons)代替CheckBoxes

1 个答案:

答案 0 :(得分:16)

很容易适应该教程,因此您可以使用RadioGroup代替CheckBox。贝娄是一个例子(我使用了RadioGroup和4 RadioButton)。首先,您必须修改Model类,以便它可以保存新数据:

public class Model {

    String question; // hold the question
    int current = NONE; // hold the answer picked by the user, initial is NONE(see below)
    public static final int NONE = 1000; // No answer selected
    public static final int ANSWER_ONE_SELECTED = 0; // first answer selected
    public static final int ANSWER_TWO_SELECTED = 1; // second answer selected
    public static final int ANSWER_THREE_SELECTED = 2; // third answer selected
    public static final int ANSWER_FOUR_SELECTED = 3; // forth answer selected

    public Model(String question) {
        this.question = question;  
    }

}

然后修改getView()方法以根据模型设置视图:

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

            if (v == null) {
                v = inflater.inflate(R.layout.the_row, parent, false);
                holder = new ViewHolder(v);
                v.setTag(holder);
                holder.group
                        .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                            public void onCheckedChanged(RadioGroup group,
                                    int checkedId) {
                                Integer pos = (Integer) group.getTag(); // To identify the Model object i get from the RadioGroup with getTag()
                                                                        //  an integer representing the actual position 
                                Model element = list.get(pos);          
                                switch (checkedId) { //set the Model to hold the answer the user picked
                                case R.id.answer0:
                                    element.current = Model.ANSWER_ONE_SELECTED;
                                    break;
                                case R.id.answer1:
                                    element.current = Model.ANSWER_TWO_SELECTED;
                                    break;
                                case R.id.answer2:
                                    element.current = Model.ANSWER_THREE_SELECTED;
                                    break;
                                case R.id.answer3:
                                    element.current = Model.ANSWER_FOUR_SELECTED;
                                    break;
                                default:
                                    element.current = Model.NONE; // Something was wrong set to the default
                                }

                            }
                        });
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.group.setTag(new Integer(position)); // I passed the current position as a tag

            holder.t.setText(list.get(position).question); // Set the question body

            if (list.get(position).current != Model.NONE) {
                RadioButton r = (RadioButton) holder.group.getChildAt(list
                        .get(position).current);
                r.setChecked(true);
            } else {
                holder.group.clearCheck(); // This is required because although the Model could have the current 
                                           // position to NONE you could be dealing with a previous row where
                                           // the user already picked an answer. 

            }
            return v;
        }

然后ViewHolder类:

class ViewHolder {
    TextView t = null;
    RadioGroup group;

    ViewHolder(View v) {
        t = (TextView) v.findViewById(R.id.textView1);
        group = (RadioGroup) v.findViewById(R.id.group_me);
    }

}

带有RadioGroup

的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/group_me"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/answer0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Ans0" />

        <RadioButton
            android:id="@+id/answer1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Ans1" />

        <RadioButton
            android:id="@+id/answer2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Ans2" />

        <RadioButton
            android:id="@+id/answer3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Ans3" />
    </RadioGroup>

</LinearLayout>