如何在自定义 ListView 适配器中处理 RadioGroup 的 onCheckedChangeListener [英] How to handle onCheckedChangeListener for a RadioGroup in a custom ListView adapter

查看:21
本文介绍了如何在自定义 ListView 适配器中处理 RadioGroup 的 onCheckedChangeListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有自定义布局的列表视图的应用程序,如下所示:
它在 RadioGroup 中有 4 个 RadioButtons 和一个 TextView.实际上,它会显示为question(TextView) 和answers(RadioButtons) 列表.上面的视图在我的自定义适配器中膨胀了,它扩展了 ArrayAdapter.

I am developing an app which has a list view with custom layout as follows :
It has 4 RadioButtons in RadioGroup and a TextView. Actually, it ll be shown as question(TextView) and answers(RadioButtons) list. Above view is inflated in my custom adapter which extends ArrayAdapter<Question>.

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

The problem is that, how should I maintain the state of RadioButtons in my custom Adapter? When RadioButton is pressed/checked and list is scrolled down, adapter automatically recycles view and the RadioButton state is lost.

  • 那么,任何人都可以提供与此相关的任何链接/信息的指南吗?
  • 或者我应该如何实现问答列表?

我关注的文章:http://www.vogella.de/articles/AndroidListView/article.html.

上面的链接使用了 CheckBox,以类似的方式我想使用 RadioGroup(RadioButtons) 而不是 CheckBoxes.

The above link is using a CheckBox, in similar manner I want to use the RadioGroup(RadioButtons) instead of CheckBoxes.

推荐答案

改编该教程很容易,因此您可以使用 RadioGroup 而不是 CheckBox.Bellow 是一个例子(我使用了一个 RadioGroup 和 4 个 RadioButton).首先,您必须修改 Model 类,以便它可以保存新数据:

It's easy to adapt that tutorial so you can use a RadioGroup instead of a CheckBox. Bellow is an example(I used a RadioGroup with 4 RadioButton). First of all you'll have to modify the Model class so it can hold the new data:

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()方法,根据模型设置视图:

Then modify the getView() method to set the views according to the model:

@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 布局:

The xml layout with a RadioGroup:

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

这篇关于如何在自定义 ListView 适配器中处理 RadioGroup 的 onCheckedChangeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆