Thursday, 29 November 2012

Android ExpandableListView with multiple CheckBoxes


Hello Android Developers.
              Here I will show you how to create ExpandableListView with multiple checkboxes . I have been trying to use CheckBox Controls But everytime I scrolling or expand the view, CheckBox state has been changed. After trying a lot, I got a solutions, may help you.


Here is code: 

1. activity_expandable_list_view.xml

<LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  >
    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>
</LinearLayout> 


2. ExpandableListViewActivity.java


public class ExpandableListViewActivity extends Activity {

ExpandableListView expandableListview;
ExpandableListViewAdapter adapter;
ArrayList<Category> category_array = new ArrayList<Category>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list_view);

// Data
for (int i = 0; i < 10; i++) {
Category category = new Category();
category.category_name = "Category - " + i;
for (int j = 0; j < 2; j++) {
SubCategory subcategory = new SubCategory();
subcategory.subcategory_name =  "  SubCategory - " + j;
subcategory.selected = true;
category.subcategory_array.add(subcategory);
}
category_array.add(category);
}
expandableListview=(ExpandableListView) findViewById(R.id.expandableListView);
adapter = new ExpandableListViewAdapter(ExpandableListViewActivity.this, expandableListview, category_array);
expandableListview.setAdapter(adapter);
 
expandableListview.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.e("Group position :: "+groupPosition, " &&   Child position :: "+childPosition);
if(category_array.get(groupPosition).subcategory_array.get(childPosition).selected) {
category_array.get(groupPosition).subcategory_array.get(childPosition).selected = false;
} else {
category_array.get(groupPosition).subcategory_array.get(childPosition).selected = true;
}
adapter.notifyDataSetChanged();
return true;
}
});
}
}

3. ExpandableListViewAdapter.java


public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ExpandableListView mExpandableListView;
private List<Category> mGroupCollection;
private int[] groupStatus;
Boolean isActive=false;

public ExpandableListViewAdapter(Context pContext, ExpandableListView pExpandableListView,
List<Category> pGroupCollection) {
mContext = pContext;
mGroupCollection = pGroupCollection;
mExpandableListView = pExpandableListView;
groupStatus = new int[mGroupCollection.size()];
setListEvent();
}
private void setListEvent() {
mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int arg0) {
groupStatus[arg0] = 1;
}
});
mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int arg0) {
groupStatus[arg0] = 0;
}
});
}
@Override
public String getChild(int arg0, int arg1) {
return mGroupCollection.get(arg0).subcategory_array.get(arg1).subcategory_name;
}
@Override
public long getChildId(int arg0, int arg1) {
return arg1;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean arg2, View convertView,ViewGroup parent)
{
final ChildHolder childHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.child_row, null);
childHolder = new ChildHolder();
childHolder.checkBox = (ImageView) convertView.findViewById(R.id.checkbox);
childHolder.name=(TextView)convertView.findViewById(R.id.childname);
convertView.setTag(childHolder);
} else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.name.setText(mGroupCollection.get(groupPosition).subcategory_array.get(childPosition).subcategory_name);

if(mGroupCollection.get(groupPosition).subcategory_array.get(childPosition).selected) {
childHolder.checkBox.setImageResource(R.drawable.checkbox_selected);
} else {
childHolder.checkBox.setImageResource(R.drawable.checkbox_normal);
}
return convertView;
}
@Override
public int getChildrenCount(int arg0) {
return mGroupCollection.get(arg0).subcategory_array.size();
}
@Override
public Object getGroup(int arg0) {
return mGroupCollection.get(arg0);
}
@Override
public int getGroupCount() {
return mGroupCollection.size();
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(int groupPosition, boolean arg1, View view, ViewGroup parent) {
GroupHolder groupHolder;

if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.group_row,null);
groupHolder = new GroupHolder();
groupHolder.img = (ImageView) view.findViewById(R.id.tab_img);
groupHolder.title = (TextView) view.findViewById(R.id.group_name);
view.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) view.getTag();
}
groupHolder.title.setText(mGroupCollection.get(groupPosition).category_name);
return view;
}
class GroupHolder {
ImageView img;
TextView title;
}
class ChildHolder {
ImageView checkBox;
TextView name;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}

There are two Object class for Sample Data :

Category.java 

public class Category {
public String category_name = null;
public ArrayList<SubCategory> subcategory_array = new ArrayList<SubCategory>();
}


SubCategory.java


public class SubCategory {
public String subcategory_name = null;
public boolean selected = false;
}

I hope this will help you.
regards
Roshni.


6 comments:

  1. pretty much enlightening!
    Thanks a lot.

    ReplyDelete
  2. can you upload the complete code ?

    ReplyDelete
  3. This tutorial is not complete... where are other layouts? I have tried to put these in eclipse along with my layouts, but it threw in error codes... Where is the source code for this? Thank you

    ReplyDelete
  4. Hi All, I have added a sample of expandable List View with checkbox. The sample also shows how to get the number of items with checked checkboxes from the main activity

    https://github.com/bhat-dinesh/ExpandableListViewWithCheckBox

    ReplyDelete