0
我在android中有一个数组适配器,它将由SurveyQuestion对象组成的一组不同的调查适配到列表视图中。RadioGroup按钮在滚动条上设置为默认值
我最初有问题,视图被回收并且答案无处不在,但现在我修复了这个问题。
但是,每当我滚动问题时,当我向后滚动时,它会再次被设置为默认答案。
如何阻止这种情况发生?
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));
}
}
我试过了,但它不起作用。看起来好像有一次我滚动onCheckedChange被再次调用,并将索引设置回0 – user3602391
那么你有ArrayAdapter中的问题列表,你的类中的另一个私人列表SurveyCTAAdapter以及存储在视图标记中的单个问题。尝试清理,以便您只使用内部ArrayAdapter数据并使用'getItem(int)'来检索和修改问题。这会让你朝正确的方向解决这个问题。 – darnmason
只要我这样做,我就能知道发生了什么。非常感谢。 – user3602391