在列表复选框随机得到选中/取消当我翻阅。安卓2.3 [英] checkboxes in the List randomly get checked/unchecked when i scroll through. android 2.3

查看:233
本文介绍了在列表复选框随机得到选中/取消当我翻阅。安卓2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的自定义适配器。有一个复选框,并在每一行文本视图。 但我有一个问题。有在列表中比其适合屏幕上的数字更多的项目。

所以,当我检查所有的复选框的屏幕上,然后向下滚动。该自动获得听之任之。

当我向上滚动,又有些乱复选框得到检查。

我知道,在getView()名单正在一次又一次地刷新和孤单的位置的问题。 但不知道该解决方案。

我使用的是布尔数组来跟踪都试过了。但二硝基甲苯知道如何正确地做到这一点。

  cb.setOnCheckedChangeListener(新OnCheckedChangeListener(){
                        @覆盖
                        公共无效onCheckedChanged(CompoundButton buttonView,
                                布尔器isChecked){

                                itemChecked [指数] =器isChecked;
                        }
                    });
                    cb.setChecked(itemChecked [指数]);
 

能否请你张贴一些code段,并详细解释。我累了自去年2周寻找这样......由于事先..

解决方案

 公开查看getChildView(最终诠释groupPosition,
            最终诠释childPosition,布尔isLastChild,查看convertView,
            ViewGroup中父){
        最后ViewHolder持有人;
        //如果(convertView == NULL){
            持有人=新ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.expandableinner_custom_row,NULL);

            holder.textView =(TextView中)convertView
                    .findViewById(R.id.textView);
            holder.checkBox =(复选框)convertView
                    .findViewById(R.id.checkBox);
            字符串str = getChild(groupPosition,childPosition)的ToString();
            holder.textView.setText(STR);
            布尔[] boolArr = SMSActivity.maintaingState.get(字符串
                    .valueOf(groupPosition));
            Log.i(BOOLARRAYSIZE,将String.valueOf(boolArr.length));

            Log.i(布尔,
                    将String.valueOf(boolArr [childPosition] +GrpPosition:
                            + groupPosition +ChildPosition:
                            + childPosition));
            如果(boolArr [childPosition]){
                holder.checkBox.setChecked(真正的);
            } 其他 {
                holder.checkBox.setChecked(假);
            }

                holder.checkBox
                        .setOnCheckedChangeListener(新OnCheckedChangeListener(){

                            @覆盖
                            公共无效onCheckedChanged(
                                    CompoundButton buttonView,
                                    布尔器isChecked){
                                // TODO自动生成方法存根

                                布尔[] boolArr = SMSActivity.maintaingState
                                        获得(将String.valueOf(groupPosition));
                                boolArr [childPosition] =器isChecked; //(对象)
                                Log.i(grpPosition
                                        将String.valueOf(childPosition));
                                Log.i(childPosition
                                        将String.valueOf(groupPosition));
                                SMSActivity.maintaingState.remove(字符串
                                        .valueOf(groupPosition));
                                SMSActivity.maintaingState.put(
                                        将String.valueOf(groupPosition)
                                        boolArr);

                                Log.i(布尔,弦乐
                                        .valueOf(boolArr [childPosition]
                                                +GrpPosition:
                                                + groupPosition
                                                +ChildPosition:
                                                + childPosition));

                            }
                });

            convertView.setTag(保持器);
        返回convertView;

    }
 

I am using my custom adapter. There is a checkbox and a text view in each row. But i have a problem. There are more items in the list than the number which fits on the screen.

So when i check any of the checkboxes on the screen and scroll down. The automatically get unchecked.

When i scroll up again some random checkboxes get checked.

I know that in getView() list is being refreshed again and again and theres an issue of the position. But dont know the solution.

I have tried using a boolean array to keep a track. but dnt know how to do it properly.

cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {

                                itemChecked[index]=isChecked;
                        }
                    });
                    cb.setChecked(itemChecked[index]);

Can you please post some code snippet and explain in detail. i am tired of searching this since last 2weeks... thanks in advance,..

解决方案

    public View getChildView(final int groupPosition,
            final int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {
        final ViewHolder holder;
        //if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.expandableinner_custom_row, null);

            holder.textView = (TextView) convertView
                    .findViewById(R.id.textView);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox);
            String str = getChild(groupPosition, childPosition).toString();
            holder.textView.setText(str);
            Boolean[] boolArr = SMSActivity.maintaingState.get(String
                    .valueOf(groupPosition));
            Log.i("BOOLARRAYSIZE ", String.valueOf(boolArr.length));

            Log.i("Boolean",
                    String.valueOf(boolArr[childPosition] + "GrpPosition: "
                            + groupPosition + "ChildPosition: "
                            + childPosition));
            if (boolArr[childPosition]) {
                holder.checkBox.setChecked(true);
            } else {
                holder.checkBox.setChecked(false);
            }

                holder.checkBox
                        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(
                                    CompoundButton buttonView,
                                    boolean isChecked) {
                                // TODO Auto-generated method stub

                                Boolean[] boolArr = SMSActivity.maintaingState
                                        .get(String.valueOf(groupPosition));
                                boolArr[childPosition] = isChecked;// (object)
                                Log.i("grpPosition",
                                        String.valueOf(childPosition));
                                Log.i("childPosition",
                                        String.valueOf(groupPosition));
                                SMSActivity.maintaingState.remove(String
                                        .valueOf(groupPosition));
                                SMSActivity.maintaingState.put(
                                        String.valueOf(groupPosition),
                                        boolArr);

                                Log.i("Boolean", String
                                        .valueOf(boolArr[childPosition]
                                                + " GrpPosition: "
                                                + groupPosition
                                                + "ChildPosition: "
                                                + childPosition));

                            }
                });

            convertView.setTag(holder);
        return convertView;

    }

这篇关于在列表复选框随机得到选中/取消当我翻阅。安卓2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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