2014-05-04 43 views
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)); 
} 

} 

回答

0

我看不出有任何需要被存儲的意見標籤您的問題。我覺得這可能是問題的一部分。我在這種情況下通常會做的是在ListView中可操作的視圖的標記中存儲Integer索引。在監聽器中,然後使用索引標籤,使用getItem(int)從ArrayAdapter中檢索對象。

+0

我試過了,但它不起作用。看起來好像有一次我滾動onCheckedChange被再次調用,並將索引設置回0 – user3602391

+0

那麼你有ArrayAdapter中的問題列表,你的類中的另一個私人列表SurveyCTAAdapter以及存儲在視圖標記中的單個問題。嘗試清理,以便您只使用內部ArrayAdapter數據並使用'getItem(int)'來檢索和修改問題。這會讓你朝正確的方向解決這個問題。 – darnmason

+0

只要我這樣做,我就能知道發生了什麼。非常感謝。 – user3602391