很容易适应该教程,因此您可以使用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>
可以告诉我更多关于“Model element = list.get(pos); “this line – rohit 2012-03-03 07:09:18
@rohit通常'OnCheckedChangeListener'不知道用户检查列表中元素的位置,所以我们设置一个'TAG'对象(一个整数)来表示你从'getView ()'方法,然后在'Model element = list.get(pos)'这一行上,我们得到了模型元素对应于用户检查某个东西的实际位置,就像该教程的第8部分,而是直接传递模型对象,我们设置标签的位置,然后检索基于该位置的模型对象。 – Luksprog 2012-03-03 07:18:36
我复制我的问题对象列表到模型对象列表中,并工作.. thx :) – rohit 2012-03-03 07:57:11