I am using expandable listview containing -
- GroupView - with one string value and checkbox
- ChildView - contains one string and checkbox .
If groupview is having children then drop down arrow would replace the checkbox. What is happening is, whenever I am selecting a checkbox in groupview and expanding the childview , all the checkboxes are getting selected.
I am aware of the fact that listview uses recycling of elements and just because of this behavior ,I need to maintain a boolean which will keep the state of checkbox in both group and childview , while populating the listview but my way of doing the same is not working.
Here mGroupCollection is my custom class arraylist which I am using for populating the list Items.
Below is my code for setting ListAdapter -
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private List<Category> mGroupCollection ;
Context ctx ;
private static final String TAG = "ExpandableListAdapter" ;
public ExpandableListAdapter(Context ctx , List<Category> mGroupCollection){
this.ctx = ctx ;
this.mGroupCollection = mGroupCollection ;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public int getChildrenCount(int groupPosition) {
return mGroupCollection.get(groupPosition).subcat_array.size();
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder ;
if(convertView==null){
convertView = LayoutInflater.from(ctx).inflate(R.layout.child_row,null);
childHolder = new ChildHolder();
childHolder.child_txt = (TextView)convertView.findViewById(R.id.child_text);
childHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(childHolder);
}else {
childHolder = (ChildHolder)convertView.getTag();
}
/* if(mGroupCollection.get(groupPosition).subcat_array.get(childPosition).selected){
childHolder.checkBox.setChecked(true);
}
else
childHolder.checkBox.setChecked(false);*/
childHolder.child_txt.setText(mGroupCollection.get(groupPosition).subcat_array.get(childPosition).subcategory_name);
childHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mGroupCollection.get(groupPosition).subcat_array.get(childPosition).selected = true ;
}
});
return convertView;
}
@Override
public Object getGroup(int groupPosition) {
return mGroupCollection.get(groupPosition);
}
@Override
public int getGroupCount() {
return mGroupCollection.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final GroupHolder holder ;
if(convertView==null){
convertView = LayoutInflater.from(ctx).inflate(R.layout.group_row,null);
holder = new GroupHolder();
holder.img = (ImageView) convertView.findViewById(R.id.bottom_arrow);
holder.textView = (TextView)convertView.findViewById(R.id.grp_text);
holder.chk_grp = (CheckBox)convertView.findViewById(R.id.checkbox_grp);
convertView.setTag(holder);
}else{
holder = (GroupHolder)convertView.getTag();
}
holder.textView.setText(mGroupCollection.get(groupPosition).category_name);
if(mGroupCollection.get(groupPosition).subcat_array.size()==0){
holder.img.setVisibility(View.GONE);
holder.chk_grp.setVisibility(View.VISIBLE);
}
else {
holder.img.setVisibility(View.VISIBLE);
holder.chk_grp.setVisibility(View.INVISIBLE);
}
if(mGroupCollection.get(groupPosition).selected){
holder.chk_grp.setChecked(true);
}
else {
Log.e(TAG,"inisde else ");
holder.chk_grp.setChecked(false);
}
holder.chk_grp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mGroupCollection.get(groupPosition).selected =true ;
Log.e(TAG,"groupPosition==="+groupPosition +",,,,ischecked==="+isChecked);
}
});
if(isExpanded) {
convertView.setBackgroundColor(ctx.getResources().getColor(R.color.colorPrimaryDark));
}
else{
convertView.setBackgroundColor(ctx.getResources().getColor(R.color.colorPrimary));
}
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public class GroupHolder{
ImageView img ;
TextView textView ;
CheckBox chk_grp ;
}
private class ChildHolder {
CheckBox checkBox ;
TextView child_txt ;
}
}
Any help is appreciated.Thanks.
Aucun commentaire:
Enregistrer un commentaire