Question

I have an array adapter in android that adapts a bunch of different surveys that are made up of SurveyQuestion objects into a listview.

I initially had the problem where views were being recycled and answers showed up everywhere, but now i've fixed that.

However every time I scroll past the question it gets set to the default answer again when I scroll back.

How do I stop this from happening?

SurveyAdapter:

public class SurveyCTAdapter extends ArrayAdapter<SurveyQuestion> {
private final Context context;//needs a way to put in check boxes
private final List<SurveyQuestion> objects;
int[] rdioIDs;

public SurveyCTAdapter(Context context, int resource, List<SurveyQuestion> objects) {
    super(context, resource, objects);
    this.context = context;
    this.objects = objects;
    rdioIDs = new int[7];
    rdioIDs[0] = R.id.r1;
    rdioIDs[1] = R.id.r2;
    rdioIDs[2] = R.id.r3;
    rdioIDs[3] = R.id.r4;
    rdioIDs[4] = R.id.r5;
    rdioIDs[5] = R.id.r6;
    rdioIDs[6] = R.id.r7;
}
public View getView(final int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(objects.get(position).rowID, null);
        RadioGroup rdiogrp = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
        rdiogrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radio, int isChecked) {
                View radioButton = radio.findViewById(isChecked);
                int radioId = radio.indexOfChild(radioButton);
                RadioButton btn = (RadioButton) radio.getChildAt(radioId);
                String selection = (String) btn.getText();
                SurveyQuestion question = (SurveyQuestion) radio.getTag();
                int index = question.buttonNames.indexOf(selection);

                if(index>=0){
                question.currentAns=index;
                Log.d("index",""+index);
                }
                notifyDataSetChanged();
            }
        });
    }
    TextView name = (TextView) convertView.findViewById(R.id.question);
    String question = objects.get(position).question;
    name.setText(question);
    RadioGroup rdiogrp = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
    for(int i=0; i<objects.get(position).buttonNames.size(); i++){
        RadioButton radioButton = (RadioButton) rdiogrp.findViewById(rdioIDs[i]);
        radioButton.setText(objects.get(position).buttonNames.get(i));
    }
    RadioButton rdiobtn = (RadioButton) rdiogrp.findViewById(rdioIDs[objects.get(position).currentAns]);
    rdiobtn.setChecked(true);
    Log.d("position",""+position);
    rdiogrp.setTag((objects).get(position));
    return convertView;
}
}

And SurveyQuestion:

public class SurveyQuestion {
public String question;
public int rowID;
public ArrayList<String> buttonNames;
public HashMap<String, Integer> answerValue;
public int currentAns=0;
public boolean needTextBox;

public SurveyQuestion(String question, boolean b){
    this.question=question;
    this.needTextBox = b;
    answerValue = new HashMap<String, Integer>();
}

public SurveyQuestion(String question, int ID){
    this.question=question;
    this.rowID = ID;
    this.needTextBox = false;
    answerValue = new HashMap<String, Integer>();

}

public SurveyQuestion(String question, int ID, ArrayList<String> names){
    this.question=question;
    this.rowID=ID;
    this.buttonNames = names;
    this.needTextBox = false;
    answerValue = new HashMap<String, Integer>();

}
public int getScore(){
    return answerValue.get(buttonNames.get(currentAns));
}

}
Was it helpful?

Solution

I don't see any need to be storing your questions in the tags of the views. I feel that this may be part of the issue. What I usually do in this scenario is store an Integer index in the tag of a view that is actionable in a ListView. In the listener then use the index tag to retrieve the object from your ArrayAdapter using getItem(int).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top