我正在开发一个具有自定义布局的列表视图的应用程序,如下所示:
它在RadioGroup中有4个Radiobuttons和一个TextVIEw.实际上,它将显示为问题(TextVIEw)和答案(Radiobuttons)列表.上面的视图在我的自定义适配器中膨胀,该适配器扩展了ArrayAdapter< Question>.
问题是,我应该如何在自定义适配器中维护Radiobutton的状态?按下/选中Radiobutton并向下滚动列表时,适配器会自动回收视图,并且Radiobutton状态将丢失.
>那么,任何人都可以指导任何关于此的链接/信息吗?
>或者我应该如何实施问题答案清单?
第一条遵循:
http://www.vogella.de/articles/AndroidListView/article.html
上面的链接使用CheckBox,类似的方式我想使用RadioGroup(Radiobuttons)而不是CheckBoxes.
解决方法:
它很容易适应该教程,因此您可以使用RadioGroup而不是CheckBox.贝娄就是一个例子(我使用了一个带有4个Radiobutton的RadioGroup).首先,您必须修改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>
总结 以上是内存溢出为你收集整理的android – 如何在自定义ListView适配器中处理RadioGroup的onCheckedChangeListener全部内容,希望文章能够帮你解决android – 如何在自定义ListView适配器中处理RadioGroup的onCheckedChangeListener所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)