Android – how to handle oncheckedchangelistener of radiogroup in a custom listview adapter
I am developing an application with a list view with a custom layout, as follows: it has four RadioButtons and a textview in the radiogroup. In fact, it will be displayed as a list of questions (textview) and answers (RadioButtons). The above view expands in my self-defined adapter, which extends arrayadapter < question >
The question is, how should I maintain the status of the RadioButton in the custom Adapter? When you press / select a RadioButton and scroll down the list, the adapter automatically recycles the view and the RadioButton status is lost
>So can anyone direct any links / information on this? > Or how should I implement the answer list?
Article 1 compliance: http://www.vogella.de/articles/AndroidListView/article.html
The link above uses Check@R_ 413_ 2419 @, in a similar way, I want to use radiogroup (RadioButtons) instead of Check@R_ 413_ 2419@es.
resolvent:
It's easy to adapt to the tutorial, so you can use radiogroup instead of Check@R_ 413_ 2419@. Bellow is an example (I used a radiogroup with four RadioButtons). First, you must modify the model class so that it can save 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;
}
}
Then modify the getview () method to set the view 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;
}
Then comes the viewholder class:
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 layout with 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>