I cannot crack this problem: I have a listview with 10 questions. For each question there are three answers (RadioButtons) that are organized within a RadioGroup. The problem is that everytime I scroll the list the RadioButtons randomly check/uncheck. I know that I have to somehow save the answers and "reload" them everytime the list is scrolled but I just can`t get it working. I have already looked on the Internet and studied these topics:
On scrolling List View radio button gets unchecked by itself If I check a radio button in listview then other radio buttons of listview get checked Radio Button gets uncked and automatically checked in listview scrolling Checking RadioButton of RadioGroup in ListView checks other item button Checkbox unchecked when I scroll listview on Android Here is my adapter code:public class adapterEdit extends ArrayAdapter<AnswerModel> {
private final ArrayList<AnswerModel> mQuestions;
private final Activity mContext;
public adapterEdit(Activity context, ArrayList<AnswerModel> questions){
super(context,0,questions);
this.mQuestions = questions;
this.mContext = context;
}
static class ViewHolder {
protected TextView text;
protected RadioButton one;
protected RadioButton two;
protected RadioButton three;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if( convertView == null) {
LayoutInflater inflater = mContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textOfQuestion);
holder.one = (RadioButton) convertView.findViewById(R.id.answerOne);
holder.one.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
mQuestions.get(getPosition).setSelected(buttonView.isChecked());
}
});
holder.two = (RadioButton) convertView.findViewById(R.id.answerTwo);
holder.two.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
mQuestions.get(getPosition).setSelected(buttonView.isChecked());
}
});
holder.three = (RadioButton) convertView.findViewById(R.id.answerThree);
holder.three.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer)buttonView.getTag();
mQuestions.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(holder);
convertView.setTag(R.id.answerOne, holder.one);
convertView.setTag(R.id.answerTwo, holder.two);
convertView.setTag(R.id.answerThree, holder.three);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.one.setTag(position);
holder.two.setTag(position);
holder.three.setTag(position);
holder.text.setText(mQuestions.get(position).getQuestion());
holder.one.setText(mQuestions.get(position).getAnswerOne());
holder.one.setChecked(mQuestions.get(position).isSelected());
holder.two.setText(mQuestions.get(position).getAnswerTwo());
holder.two.setChecked(mQuestions.get(position).isSelected());
holder.three.setText(mQuestions.get(position).getAnswerThree());
holder.three.setChecked(mQuestions.get(position).isSelected());
return convertView;}}
Here is the class of the object that I'm using in the listview:
public class AnswerModel {
private String mQuestion;
private String mAnswerOne;
private String mAnswerTwo;
private String mAnswerThree;
private boolean selected = false;
public AnswerModel (String question, String answerOne, String answerTwo, String answerThree) {
this.mQuestion = question;
this.mAnswerOne = answerOne;
this.mAnswerTwo = answerTwo;
this.mAnswerThree = answerThree;
}
public String getQuestion(){return mQuestion;}
public String getAnswerOne(){return mAnswerOne;}
public String getAnswerTwo(){return mAnswerTwo;}
public String getAnswerThree(){return mAnswerThree;}
public boolean isSelected() {return selected;}
public void setSelected(boolean selected) {
this.selected = selected;
}}
And the XML of the list item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.harrypotterfanquiz.MainActivity">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textOfQuestion"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_marginTop="10dp"
android:background="@color/questionColor"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/questionOne"
android:textAppearance="@style/appearanceBody" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<RadioButton
android:id="@+id/answerOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/questionOneA" />
<RadioButton
android:id="@+id/answerTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/questionOneB" />
<RadioButton
android:id="@+id/answerThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/questionOneC" />
</RadioGroup>
</LinearLayout>
I know that the problem is somwhere in adapter. I've been trying to solve this for over a week. If you have any advice or solution I will be very grateful. Thank you.