3

一个RadioGroup中我开发了具有自定义布局列表视图如下应用:
它在RadioGroup和。实际上,它将被显示为问题TextView)和答案RadioButtons)列表。以上视图在我的自定义适配器中膨胀,该适配器扩展了ArrayAdapter<Question>如何处理onCheckedChangeListener在自定义的ListView适配器

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

  • 那么,任何人都可以引导任何关于此的链接/信息?
  • 或者我应该如何执行问题答案列表?

Article I follow: http://www.vogella.de/articles/AndroidListView/article.html

上述链路是使用CheckBox,以类似的方式我想使用RadioGroupRadioButtons)代替CheckBoxes

回答

15

很容易适应该教程,因此您可以使用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); 
    } 

} 

的XML布局与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> 
+0

可以告诉我更多关于“Model element = list.get(pos); “this line – rohit 2012-03-03 07:09:18

+1

@rohit通常'OnCheckedChangeListener'不知道用户检查列表中元素的位置,所以我们设置一个'TAG'对象(一个整数)来表示你从'getView ()'方法,然后在'Model element = list.get(pos)'这一行上,我们得到了模型元素对应于用户检查某个东西的实际位置,就像该教程的第8部分,而是直接传递模型对象,我们设置标签的位置,然后检索基于该位置的模型对象。 – Luksprog 2012-03-03 07:18:36

+0

我复制我的问题对象列表到模型对象列表中,并工作.. thx :) – rohit 2012-03-03 07:57:11

相关问题