【问题标题】:RadioGroup buttons loosing it's state after listView scroll列表视图滚动后 RadioGroup 按钮失去状态
【发布时间】:2013-12-04 20:26:30
【问题描述】:

我遇到了一个常见问题,但我看不到解决方案。 我想要实现的是在每个问题下显示问题列表(它们来自解析的 JSON)和 radioGroup。

我已经设法显示问题列表。现在,当我检查按钮并向下滚动列表时,上面的按钮正在失去它的状态。我关注并阅读了 getView() 并尝试了不同的方法:

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

我在我的 ArrayAdapter 中使用 HashMap。我试图将这些“转换”为对象数组,但没有成功。我不知道如何使每个按钮保持其状态。

你能指出我做错了什么或者我错过了什么吗???

我已经包含了 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;
    }

}

【问题讨论】:

  • 我在滚动列表视图时遇到了类似的编辑文本丢失内容的问题。找不到解决方案,最终循环膨胀视图并将它们添加到 ScrollView。

标签: android android-listview android-arrayadapter


【解决方案1】:

供日后参考。使用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;
}

【讨论】:

  • 拯救了我的一天 :) 非常感谢兄弟 :)
  • 嗨@SIVAKUMAR.J 检查的是SparseIntArray。您可以在 onCreate 回调中定义它并在自定义适配器的 getView() 中使用它。
猜你喜欢
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多