RadioGroup按钮在滚动时设置为默认值

时间:2014-05-04 21:45:57

标签: android radio-group

我在android中有一个数组适配器,它将一组由SurveyQuestion对象组成的不同调查适应到列表视图中。

我最初遇到的问题是视图被回收,答案随处可见,但现在我已经解决了这个问题。

然而,每当我滚过问题时,当我向后滚动时,它会再次设置为默认答案。

如何阻止这种情况发生?

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;
}
}

和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));
}

}

1 个答案:

答案 0 :(得分:0)

我没有看到任何需要将您的问题存储在视图的标记中。我觉得这可能是问题的一部分。我在这种情况下通常做的是将一个Integer索引存储在一个可在ListView中操作的视图的标记中。在侦听器中,然后使用index标签使用getItem(int)从ArrayAdapter中检索对象。