RadioGroup buttons loosing it's state after listView scroll

一个人想着一个人 提交于 2019-12-04 12:56:06

For future reference. Use the SparseIntArray. In my case I called it checked.

@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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!