lundi 6 novembre 2017

How to persist Checkbox checked state in ExpandableListView in android ?

i am using Expandablelistview with multiple checkbox inside child,but i am not able to maintain checked states of checkbox inside each group.i tried and searched a lot and got some code but still getting this issues.i think sometimes i am not getting right child position or group position due to which couldn't persist their checked state.i have already waste our 2 days in this troubleshooting. so Your help would really saved my coming days and appreciated.This is My Adapter Code :

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);

    }

}

}




Aucun commentaire:

Enregistrer un commentaire