在勾选复选框后滚动时,复选框会被随机选中并取消选中

时间:2014-04-15 08:46:07

标签: java android expandablelistview expandablelistadapter

    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

        }

}

我遇到了问题。当我检查子列表中的复选框并向上和向下滚动时,另一个孩子中的复选框会被随机选中并取消选中。 我无法确定问题所在 请帮忙

2 个答案:

答案 0 :(得分:0)

我没有完成上述所有代码,但我认为列表视图单元的重用存在问题。我有同样的问题。尝试使用持有者模式来存储有关每个单元格的信息。 在我看来,本教程非常好 http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder

答案 1 :(得分:0)

您应该在collections数组中维护状态(Checked / Unchecked)。由于Adapters的概念是重用视图,因此只要您滚动列表,它就会重复使用屏幕外的先前视图。

因此,您应该使用唯一变量位置维护一个Array来存储check / uncheck状态。使用此数组可以恢复getView方法中的先前状态。