如何在ExpandableListView中保持Checkbox选中状态?

时间:2017-11-06 12:29:36

标签: android checkbox expandablelistview expandablelistadapter

我正在使用Expandablelistview内部多个checkbox子项,但我无法在每个组内维护checkbox的已检查状态。我经常尝试搜索并获得了一些代码但仍然得到这个问题。我认为有时我没有得到正确的孩子的位置或团体位置,因为这不能坚持他们的检查状态。我已经浪费了我们2天的故障排除。所以你的帮助真的可以保存我未来的日子并受到赞赏。这是我的适配器代码:

    public class SubItemAdapter extends BaseExpandableListAdapter {

    private List<String> titleList;
    private List<ItemDetailMapper> number_items;
    private LayoutInflater inflater;
    private HashMap<String,List<ItemDetailMapper>> childlist;
    Context context;
    FontFamily fontFamily;
    private RadioButton mSelectedRB;
    private int mSelectedPosition = -1;
    ArrayList<Integer> integerArrayList;
    boolean [] checkedPosition;
    boolean [] checkParentPosition;
    HashMap<Integer, boolean []> check;
    ImageLoader imageLoader;
    DisplayImageOptions displayImageOptions;
    int count = 0;
    Integer numberof_items;
    List<Integer> child_position;
    List<Integer> parent_position;
    private HashMap<Integer,boolean[]> itemCheck;
    public SubItemAdapter(ItemDetailActivity context, List<String> headings_itemList, HashMap<String, List<ItemDetailMapper>> child_itemList, List<ItemDetailMapper> child) {
        this.context = context;
        this.titleList = headings_itemList;
        this.childlist = child_itemList;
        this.number_items = child;
        fontFamily = new FontFamily(context);
        integerArrayList = new ArrayList<Integer>();
        imageLoader = ImageLoader.getInstance();
        checkParentPosition = new boolean[titleList.size()];
        check = new HashMap<Integer, boolean []>();
        child_position = new ArrayList<Integer>();
        parent_position = new ArrayList<Integer>();
        itemCheck = new HashMap<Integer, boolean[]>();
        displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(android.R.drawable.ic_menu_gallery)
                .showImageForEmptyUri(android.R.drawable.ic_menu_gallery)
                .showImageOnFail(android.R.drawable.ic_menu_gallery)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

    }


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

    @Override
    public int getChildrenCount(int groupPosition) {
        return childlist.get(titleList.get(groupPosition)).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childlist.get(this.titleList.get(groupPosition)).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
       String listTitle = (String) getGroup(groupPosition);

        CoachHolder holder = null;
        View row = convertView;

        if (row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.shopping_list_item_parentrow, null);
            holder = new CoachHolder(row);
            row.setTag(holder);
        }else{
            holder =(CoachHolder)row.getTag();
        }
        String numberofitems = 
        number_items.get(groupPosition).getSubProductItemNumber();
        numberof_items =    
     Integer.valueOf(number_items.get(groupPosition).getSubProductItemNumber());
        Boolean requiredproduct = 
     number_items.get(groupPosition).getSubProductRequired();

             return row;
    }

    @Override
    public View getChildView(int groupPosition,int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemDetailMapper expandedListData = (ItemDetailMapper)getChild(groupPosition,childPosition);

       // final String expandedListText = (String) getChild(groupPosition, childPosition);
       // checkedPosition = new boolean[childlist.get(titleList.get(groupPosition)).size()];
        ChildHolder holder = null;
        View row = convertView;
        LinearLayout lnrLayout = null;

       final int chl = childPosition;
       final int grp= groupPosition;


        if (row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.sub_item_row, null);
            holder = new ChildHolder(row);
            row.setTag(R.layout.sub_item_row,holder);

        }else{
            holder =(ChildHolder)row.getTag(R.layout.sub_item_row);

        }

         if (itemCheck.containsKey(grp)){
             boolean getChecked[] = itemCheck.get(grp);
             holder.checkBox.setChecked(getChecked[chl]);
             notifyDataSetChanged();
         }else{
             boolean getChecked[] = new boolean[getChildrenCount(grp)];
             itemCheck.put(grp,getChecked);
             holder.checkBox.setChecked(false);
             notifyDataSetChanged();
         }

         holder.checkBox.setOnCheckedChangeListener(null);



        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                 if (b){
                     boolean getChecked[] = itemCheck.get(grp);
                     getChecked[chl] = b;
                     itemCheck.put(grp,getChecked);
                     notifyDataSetChanged();

                 }else{
                     boolean getChecked[] = itemCheck.get(grp);
                     getChecked[chl] = b;
                     itemCheck.put(grp,getChecked);
                     notifyDataSetChanged();
                 }
             }
         });

        holder.child_tv1.setText(expandedListData.getSubproductItemName());
        holder.item_price.setText("("+"$ "+ 
        expandedListData.getSubProductItemPrice()+")");
        imageLoader.displayImage(expandedListData.getSubProductItemImage(), 
        holder.imagev1, displayImageOptions);

        return row;
    }



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


    static class CoachHolder {

        TextView textView1;
        ImageView imageView;

        //TextView descriptions
        public CoachHolder(View v)
        {

            textView1 = (TextView)v.findViewById(R.id.parenttext);
            imageView = (ImageView)v.findViewById(R.id.expandable_identifier);

        }

    }



    //CUSTOM Child CLASS for listgroup
    static class ChildHolder {

        ImageView imagev1;
        TextView child_tv1,item_price;

     //   RadioGroup radioGroup;
        CheckBox checkBox;


        //TextView descriptions
        public ChildHolder(View v)
        {
            item_price = (TextView)v.findViewById(R.id.item_price);
            imagev1 = (ImageView)v.findViewById(R.id.imageView20);
            child_tv1 = (TextView)v.findViewById(R.id.textView164);
            checkBox = (CheckBox) v.findViewById(R.id.radioButton5);

        }

    }

}

0 个答案:

没有答案