2013-11-20 42 views
3

我努力與一個共同的問題,但我看不到它的解決方案。 我想要實現的是顯示問題列表(它們來自解析的JSON)和radioGroup。 enter image description hereRadioGroup按鈕失去它的狀態後列表視圖滾動

我已經設法得到問題列表顯示。現在,當我檢查按鈕並向下滾動列表時,上面的按鈕正在失去狀態。我按照和閱讀getView()和tryed不同的方式:

stackoverflow.com/...how-to-handle-oncheckedchangelistener-for-a-radiogroup-in-a-custom-listview-adap

這裏:

stackoverflow.com...android-how-to-make-radiogroup-work-correctly-in-a-listview

我使用HashMap的在我的ArrayAdapter。我試圖將這些「轉換」爲對象數組,但沒有成功。我不知道如何讓每個按鈕保持它的狀態。

你能指出我做錯了什麼或我失蹤了嗎?

我已經包含了arrayAdater的代碼和我的xml可視化佈局的模型。

public class QuestionListAdapter extends ArrayAdapter<HashMap<String, Object>> { 
    PlayerData plData = new PlayerData(); 
    public int current = NONE; 
    public static final int NONE = 1000; 
    public static String id; 
    private Activity _activity; 
    private ArrayList<HashMap<String, Object>> _questionsList; 
    private HashMap<String, Object> answers; 

    public QuestionListAdapter(Activity activity, int resource, 
      ArrayList<HashMap<String, Object>> questionsList) { 
     super(activity, resource, questionsList); 
     _activity = activity; 
     _questionsList = questionsList; 
    } 

    public static final class ViewHolder { 
     TextView question; 
     RadioGroup radioG; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     ViewHolder holder = null; 

     if (view == null) { 
      LayoutInflater layoutInflater = (LayoutInflater) _activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = layoutInflater.inflate(R.layout.question_right_fragment, 
        null); 
      holder = new ViewHolder(); 
      holder.question = (TextView) view.findViewById(R.id.question); 
      holder.radioG = (RadioGroup) view.findViewById(R.id.radio0); 
      view.setTag(holder); 
     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 
     final HashMap<String, Object> pData = _questionsList.get(position); 
     if (pData != null) { 
      holder.question.setText((String) pData.get("questionText")); 

      // holder.radioG.clearCheck(); 
      holder.radioG 
        .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

         @Override 
         public void onCheckedChanged(RadioGroup group, 
           int checkedId) { 
          Integer pos = (Integer) group.getTag(); 

          switch (checkedId) { 

          case R.id.radio1: 
           plData.setQ1((byte) 1); 
           break; 

          case R.id.radio2: 
           plData.setQ2((byte) 2); 
           break; 

          case R.id.radio3: 
           plData.setQ3((byte) 3); 
           break; 

          case R.id.radio4: 
           plData.setQ4((byte) 4); 
           break; 

          case R.id.radio5: 
           plData.setQ5((byte) 5); 
           break; 

          case R.id.radio6: 
           plData.setQ6((byte) 6); 
           break; 

          case R.id.radio7: 
           plData.setQ1((byte) 7); 
           break; 

          default: 

          } 
          Log.d("IN PLDATA:", plData.toString()); 
          answers.put("id", plData); 

         } 
        }); 
     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 

     holder.radioG.setTag(new Integer(position)); 
     if (_questionsList.get(position).get(
       holder.radioG.getCheckedRadioButtonId()) != null) { 
      RadioButton r = (RadioButton) holder.radioG 
        .getChildAt((Integer) _questionsList.get(position).get(
          holder.radioG.getCheckedRadioButtonId())); 
      r.setChecked(true); 
     } else { 
      holder.radioG.clearCheck(); 
     } 

     return view; 
    } 

} 
+0

我在滾動列表視圖時遇到了編輯文本丟失內容的類似問題。找不到解決方案並最終在循環中添加視圖並將其添加到ScrollView。 – developeralways

回答

4

供將來參考。使用SparseIntArray。在我的情況下,我打電話給它檢查。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    ViewHolder holder = null; 

    if (convertView == null) { 
     LayoutInflater mInflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = mInflater.inflate(R.layout.question_right_fragment, null); 
     holder = new ViewHolder(); 
     holder.question = (TextView) convertView.findViewById(R.id.question); 
     holder.radioG = (RadioGroup) convertView.findViewById(R.id.radio0); 

     convertView.setTag(holder); 


    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    final HashMap<String, Object> pData = _questionsList.get(position); 
    if (pData != null){ 
     holder.question.setText((String)pData.get("questionText")); 
    }else{ 
     holder.question.setText("");  
    } 

    holder.radioG.setOnCheckedChangeListener(null); 
    holder.radioG.clearCheck(); 

    if(checked.indexOfKey(position)>-1){ 
     holder.radioG.check(checked.get(position)); 
    }else{ 
     holder.radioG.clearCheck(); 
    } 
    holder.radioG.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if(checkedId>-1){ 
       checked.put(position, checkedId); 
      }else{ 
       if(checked.indexOfKey(position)>-1) 
        checked.removeAt(checked.indexOfKey(position)); 
      } 
     } 
    }); 

    return convertView; 
} 
+0

保存我的一天:)非常感謝兄弟:) – MohanRaj

+1

在上面的src代碼是什麼「檢查」 –

+0

嗨@ SIVAKUMAR.J檢查是SparseIntArray。你可以在你的onCreate回調中定義它,並在你的自定義適配器的getView()中使用它。 – MohanRaj

相關問題