简体   繁体   中英

checkboxes gets randomly checked and unchecked when i scroll after checking a checkbox

    public class CustomExpandableListAdapter extends BaseExpandableListAdapter implements OnCheckedChangeListener {

        private CoreWorkFlowActivity context;
        private List<ActivationType> listDataHeader=new ArrayList<ActivationType>(); // header titles
        // child data in format of header title, child title
        ArrayList<Integer> arrIVRetest = new ArrayList<Integer>();
        private HashMap<String, List<OrderLineItem>> listDataChild= new HashMap<String, List<OrderLineItem>>();
        public static int count=0;
        public static int countIVRetest=0;
        public CustomExpandableListAdapter(CoreWorkFlowActivity context, ArrayList<ActivationType> listDataHeader,
                HashMap<String, List<OrderLineItem>> listDataChild) {
            this.context = context;
            this.listDataHeader = listDataHeader;
            this.listDataChild = listDataChild;
        }

        @Override
        public Object getChild(int groupPosition, int childPosititon) {

            return this.listDataChild.get(this.listDataHeader.get(groupPosition).getName())
                    .get(childPosititon).getProductName().getValue().toString();
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {

            return childPosition;
        }
        ArrayList<Integer> arrActivation = new ArrayList<Integer>();

        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) 
        {
            //listDataHeader.clear();
            //arrIVRetest.clear();
            //listDataChild.clear();

            final OrderLineItem childObj=this.listDataChild.get(this.listDataHeader.get(groupPosition).getName()).get(childPosition);
            final String childText = (String) getChild(groupPosition, childPosition);
            final String groupName=this.listDataHeader.get(groupPosition).getName();

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.olilist_expanded, null);
            }

            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.oliexpandedTextView);

            txtListChild.setText(childText);
            txtListChild.setTypeface(RobotoTypeface.sRobotoRegular(context));
            TextView txtlistStatus=(TextView)convertView.findViewById(R.id.oliexpandedstatus);
            txtlistStatus.setTypeface(RobotoTypeface.sRobotoItalic(context));
            CheckBox cbChild=(CheckBox)convertView.findViewById(R.id.checkboxoli);
            cbChild.setOnCheckedChangeListener(new OnCheckedChangeListener() 
            {


                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    //IVRETEST
                    if(isChecked && groupName.equals(OLIConstants.IV_RETEST))
                    {

                        countIVRetest=countIVRetest+1;
                        arrIVRetest.add(childPosition);//adding oli which r checked 
                        context.getSummaryFragment().getOli2(arrIVRetest,groupPosition);

                        if(!context.getSummaryFragment().ivretestAll.isEnabled())
                        {
                            context.getSummaryFragment().enableButtonIVRetest(true);
                        }

                    }
                    else if(!isChecked && groupName.equals(OLIConstants.IV_RETEST))
                    {
                        countIVRetest=countIVRetest-1;
                        Integer arrv = arrIVRetest.indexOf(childPosition);
                        arrIVRetest.remove(arrv);
                        context.getSummaryFragment().getOli2(arrIVRetest,groupPosition);
                        if(context.getSummaryFragment().ivretestAll.isEnabled() && countIVRetest<=0)
                        {
                            context.getSummaryFragment().enableButtonIVRetest(false);
                        }
                    }




                    //Activation
                    if(isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE))
                    {
                        count=count+1;
                        arrActivation.add(childPosition);
                        context.getSummaryFragment().getOli(arrActivation,groupPosition);

                        if(!context.getSummaryFragment().activateSystem.isEnabled())
                        {
                            context.getSummaryFragment().enableButton(true);
                        }

                    }
                    else if(!isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE))
                    {
                        count=count-1;

                        arrActivation.remove(arrActivation.indexOf(childPosition));
                        context.getSummaryFragment().getOli(arrActivation,groupPosition);
                        if(context.getSummaryFragment().activateSystem.isEnabled() && count<=0)
                        {
                            context.getSummaryFragment().enableButton(false);
                        }


                    }

                }

            });


            ProgressBar pbchild=(ProgressBar)convertView.findViewById(R.id.progressBaroli);
            ImageView backArrow=(ImageView)convertView.findViewById(R.id.backwardArrow);


            if(groupName.equals(OLIConstants.READY_TO_ACTIVATE)||groupName.equals(OLIConstants.ACTIVATED_EQUIPMENT) || groupName.equals(OLIConstants.NO_ACTION)||groupName.equals(OLIConstants.COMPLETED_WORK_ITEMS)||groupName.equals(OLIConstants.PENDING_ITEMS))
            {
                backArrow.setVisibility(View.GONE);

            }
            else//added
            {
                backArrow.setVisibility(View.VISIBLE);
            }

            if(groupName.equals(OLIConstants.IV_RETEST))
            {

                txtlistStatus.setVisibility(View.VISIBLE);
                txtlistStatus.setText(childObj.getStatus().getValue().toString());
            }
            else
            {
                txtlistStatus.setVisibility(View.GONE);
            }
    //


            if(!childObj.isSelectable())
            {
                cbChild.setVisibility(View.GONE);
                pbchild.setVisibility(View.GONE);
            }
            else if(childObj.isSelectable() == true && (groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)||groupName.equals(OLIConstants.IV_RETEST)) )
            {
                cbChild.setVisibility(View.VISIBLE);
                //childObj.setSelectable(false); 
                if(groupName.equals(OLIConstants.IV_RETEST))
                {
                    if(childObj.isChecked()==true)//giving this only for ivretest
                    {
                        if(childObj.getStatus().getValue().toString().equalsIgnoreCase("Open") && childObj.getTechAction().getValue().toString().equalsIgnoreCase("IV Retest"))
                        {
                            cbChild.setChecked(true);
                            childObj.setChecked(false);//for making it not to select again
                        }
                        else{
                            cbChild.setChecked(false);
                        }

                        if(countIVRetest>0)
                        {context.getSummaryFragment().ivretestAll.setEnabled(true);}
                    }
                    else{
                        cbChild.setChecked(false);

                    }
                }

                /*else{
                    cbChild.setChecked(false);
                }*/

            }
            //***************************************************************************************
            /*else if(childObj.isSelectable() == true && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)) 
            {

                cbChild.setVisibility(View.VISIBLE);
                childObj.setChecked(false);

            }
            else if(childObj.isSelectable() == true && groupName.equals(OLIConstants.IV_RETEST) )
            {
                cbChild.setVisibility(View.VISIBLE);
                if(groupName.equals(OLIConstants.IV_RETEST))
                {
                    if(childObj.isChecked()==true)//giving this only for ivretest
                    {
                        if(childObj.getStatus().getValue().toString().equalsIgnoreCase("Open") && childObj.getTechAction().getValue().toString().equalsIgnoreCase("IV Retest"))
                        {
                            cbChild.setChecked(true);
                            childObj.setChecked(false);//for making it not to select again
                        }
                        else{
                            cbChild.setChecked(false);
                        }

                        if(countIVRetest>0)
                        {context.getSummaryFragment().ivretestAll.setEnabled(true);}
                    }
                    else{
                        cbChild.setChecked(false);

                    }
                }


            }*/
            //********************************************************************************


            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return this.listDataChild.get(this.listDataHeader.get(groupPosition).getName())
                    .size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return this.listDataHeader.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return this.listDataHeader.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            ActivationType headerTitleValues = (ActivationType) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.summarylist_layout, null);
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.summary_text);
            lblListHeader.setTypeface(RobotoTypeface.sRobotoBold(context));
            lblListHeader.setText(headerTitleValues.getName());
            lblListHeader.setTextColor(Color.parseColor(headerTitleValues.getColor()));

            ImageView lblimage=(ImageView)convertView.findViewById(R.id.summarylist_color);
            lblimage.setBackgroundColor(Color.parseColor(headerTitleValues.getColor()));

            ImageView arrowImg=(ImageView)convertView.findViewById(R.id.arrowImg);
            if(isExpanded)
            {
                arrowImg.setBackgroundResource(R.drawable.up_arrow);
            }
            else
            {
                arrowImg.setBackgroundResource(R.drawable.down_arrow);
            }

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

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

        }

}

i am having a problem.when i check a checkbox in a childlist and scroll up and down the checkboxes in the other child are randomly getting selected and deselected. i am not being able to figure where the problem lies Please help

I didn't go through all the above code, but I believe there's something wrong with the reuse of list view cells. I had the exact same problem. Try use the holder pattern to store information about each individual cell. This tutorial is really good in my opinion http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder

You should maintain the states (Checked/Unchecked) in the collections array. As the concept of Adapters is to reuse the views, As soon as you scroll the list, it will reuse the previous views which went out of the screen.

So, You should maintain an Array to store the check/uncheck state using the unique variable position. Use this array to restore the previous state in getView method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM