checkboxes in the List randomly get checked/unchecked when i scroll through. android 2.3

旧城冷巷雨未停 提交于 2020-01-01 07:12:11

问题


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,..


回答1:


    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:


set each check box position using settag(position) in getview() method and while clicking each checkbox get the position by (Integer)buttonView.getTag(). I think below code will solve your issue.

    public View getView(int position, View convertView, ViewGroup parent)
    {
        holder=new ViewHolder();
        convertView=lInflater.inflate(R.layout.extraevents, null);
        holder.txtTitle=(TextView)convertView.findViewById(R.id.eventtitle);
        holder.txtLocation=(TextView)convertView.findViewById(R.id.eventLoc);
        holder.ChkEachBox=(CheckBox)convertView.findViewById(R.id.CheckBoxSelected);            

        holder.ChkEachBox.setClickable(true);           
        holder.ChkEachBox.setFocusable(false);  
        holder.ChkEachBox.setTag(position);

        holder.ChkEachBox.setOnCheckedChangeListener(this);
        EventItems cart=eventList.get(position);

        holder.txtTitle.setText(cart.title);
        holder.txtLocation.setText(cart.location);          

        if(cart.selected==true)
        {
            holder.ChkEachBox.setChecked(true);             
        }
        else
        {
            holder.ChkEachBox.setChecked(false);
        }           

        return convertView;
    }   

    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) 
    {                       
        EventItems cart=eventList.get((Integer)buttonView.getTag());                            
        if(buttonView.isChecked())
        {
            cart.selected = true;               
        }
        else 
        {               
            cart.selected = false;

        }   
    }       


来源:https://stackoverflow.com/questions/5715630/checkboxes-in-the-list-randomly-get-checked-unchecked-when-i-scroll-through-and

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