RadioGroup中ListView控件按钮滚动后失去它的状态 [英] RadioGroup buttons loosing it's state after listView scroll

查看:145
本文介绍了RadioGroup中ListView控件按钮滚动后失去它的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个共同的问题,然而奋斗我看不到解决的办法。
我试图做到的,是要显示的问题,根据每个列表(他们来自解析的JSON)和radioGroup中。

我设法得到显示的问题清单。现在,当我检查按钮和向下滚动按钮上正在失去它的状态列表。我按照和阅读getView(),并tryed不同的方式:

<一个href=\"http://stackoverflow.com/questions/9392511/how-to-handle-oncheckedchangelistener-for-a-radiogroup-in-a-custom-listview-adap\">stackoverflow.com/...how-to-handle-oncheckedchangelistener-for-a-radiogroup-in-a-custom-listview-adap

在这里:

<一个href=\"http://stackoverflow.com/questions/2937581/android-how-to-make-radiogroup-work-correctly-in-a-listview\">stackoverflow.com...android-how-to-make-radiogroup-work-correctly-in-a-listview

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

您能指出我在做什么错误或我缺少的是什么?

我已经包括了code为arrayAdater和我的XML可视化布局的样机。

 公共类QuestionListAdapter扩展ArrayAdapter&LT;&HashMap的LT;弦乐,对象&gt;&GT; {
    PlayerData plData =新PlayerData();
    公众诠释电流=无;
    公共静态最终诠释无= 1000;
    公共静态字符串ID;
    私人活动_activity;
    私人的ArrayList&LT;&HashMap的LT;弦乐,对象&gt;&GT; _questionsList;
    私人的HashMap&LT;弦乐,对象&gt;答案;    公共QuestionListAdapter(活动活动,INT资源,
            ArrayList的&LT;&HashMap的LT;弦乐,对象&gt;&GT; questionsList){
        超(活动,资源,questionsList);
        _activity =活动;
        _questionsList = questionsList;
    }    公共静态final类ViewHolder {
        TextView的问题;
        RadioGroup中radioG;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看查看= convertView;
        ViewHolder支架=无效;        如果(查看== NULL){
            LayoutInflater layoutInflater =(LayoutInflater)_activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            鉴于= layoutInflater.inflate(R.layout.question_right_fragment,
                    空值);
            持有人=新ViewHolder();
            holder.question =(TextView中)view.findViewById(R.id.question);
            holder.radioG =(RadioGroup中)view.findViewById(R.id.radio0);
            view.setTag(保持器);
        }其他{
            支架=(ViewHolder)view.getTag();
        }
        最终的HashMap&LT;弦乐,对象&gt;的pData = _questionsList.get(位置);
        如果(pData所!= NULL){
            holder.question.setText((字符串)pData.get(questionText));            // holder.radioG.clearCheck();
            holder.radioG
                    .setOnCheckedChangeListener(新RadioGroup.OnCheckedChangeListener(){                        @覆盖
                        公共无效onCheckedChanged(RadioGroup中组,
                                INT checkedId){
                            整数POS =(整数)group.getTag();                            开关(checkedId){                            案例R.id.radio1:
                                plData.setQ1((字节)1);
                                打破;                            案例R.id.radio2:
                                plData.setQ2((字节)2);
                                打破;                            案例R.id.radio3:
                                plData.setQ3((字节3)3);
                                打破;                            案例R.id.radio4:
                                plData.setQ4((字节)4);
                                打破;                            案例R.id.radio5:
                                plData.setQ5((字节)5);
                                打破;                            案例R.id.radio6:
                                plData.setQ6((字节)6);
                                打破;                            案例R.id.radio7:
                                plData.setQ1((字节)7);
                                打破;                            默认:                            }
                            Log.d(IN PLDATA:,plData.toString());
                            answers.put(ID,plData);                        }
                    });
        }其他{
            支架=(ViewHolder)view.getTag();
        }        holder.radioG.setTag(新的整数(位置));
        如果(_questionsList.get(位置)获得(
                holder.radioG.getCheckedRadioButtonId())!= NULL){
            单选R =(单选)holder.radioG
                    .getChildAt((整数)_questionsList.get(位置)获得(
                            holder.radioG.getCheckedRadioButtonId()));
            r.setChecked(真);
        }其他{
            holder.radioG.clearCheck();
        }        返回视图。
    }}


解决方案

有关备查。使用 SparseIntArray 。在我来说,我把它叫做检查。

  @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){    ViewHolder支架=无效;    如果(convertView == NULL){
        LayoutInflater mInflater =(LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.question_right_fragment,NULL);
        持有人=新ViewHolder();
        holder.question =(TextView中)convertView.findViewById(R.id.question);
        holder.radioG =(RadioGroup中)convertView.findViewById(R.id.radio0);        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }
    最终的HashMap&LT;弦乐,对象&gt;的pData = _questionsList.get(位置);
    如果(pData所!= NULL){
        holder.question.setText((字符串)pData.get(questionText));
    }其他{
        holder.question.setText();
    }    holder.radioG.setOnCheckedChangeListener(NULL);
    holder.radioG.clearCheck();    如果(checked.indexOfKey(位置)-1个){
        holder.radioG.check(checked.get(位置));
    }其他{
        holder.radioG.clearCheck();
    }
    holder.radioG.setOnCheckedChangeListener(新OnCheckedChangeListener(){        @覆盖
        公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
            如果(checkedId -1个){
                checked.put(位置,checkedId);
            }其他{
                如果(checked.indexOfKey(位置)-1个)
                    checked.removeAt(checked.indexOfKey(位置));
            }
        }
    });    返回convertView;
}

I struggle with a common problem however I can't see the solution to it. What I'm trying to achieve is to display the list of questions (they come from parsed JSON) and radioGroup under each.

I've manage to get questions list displayed. Now when I check the buttons and scroll down the list the upper buttons are loosing it state. I have followed and read about getView() and tryed different ways:

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

and here:

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

I'm using the HashMap in my ArrayAdapter. I was trying to 'convert' these to array of objects but without success. I have no idea how to make each of the buttons to keep it's state.

Can you point out what I'm doing wrong or what I'm missing???

I've included the code for arrayAdater and a mockup of my xml visual layout.

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;
    }

}

解决方案

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;
}

这篇关于RadioGroup中ListView控件按钮滚动后失去它的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆