Reputation: 374
My Scenario:
I have a list in groupView and childView of Expandable list. GroupView and ChildView has checkboxes. I have no problem setting the checkboxes. I want to check all the checkboxes of ChilView when the checkbox in GroupView is checked.
I have gone through these two solutions:
Android ExpandableListView CheckBox Select Only Clicked Checkbox
and
Check all checkbox in ExpandableListView (Android)
I check a checkbox say in groupPosition 2 then the checkbox in say groupPosition 5 automatically gets checked. Again when I check a checkbox say in groupPosition 1 then the checkbox in say groupPosition 6 automatically gets checked. I have saved the states of checkbox in Hash Map. Can anybody explain why is this happening.
My Code For childrenView are as follows
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ChildHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.group_item, null);
holder = new ChildHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
Item item = getChild(groupPosition, childPosition);
holder.title.setText(item.name);
if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true)
holder.cb.setChecked(true);
else {
holder.cb.setChecked(false);
}
return convertView;
}
My Code for groupView are as follows
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final GroupHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.group_list, null);
holder = new GroupHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
holder.imageView = (ImageView) convertView
.findViewById(R.id.label_indicator);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.imageView
.setImageResource(groupStatus[groupPosition] == 0 ? R.drawable.group_down
: R.drawable.group_up);
Item groupItem = getGroup(groupPosition);
holder.title.setText(groupItem.name);
if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true) {
holder.cb.setChecked(true);
} else {
holder.cb.setChecked(false);
}
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
DataHolder.selectedGroupMembers.put(groupPosition,
new Boolean(true));
Log.i("Prativa Clicked", "group" + groupPosition);
notifyDataSetChanged();
} else if (DataHolder.selectedGroupMembers
.containsKey(groupPosition)) {
DataHolder.selectedGroupMembers.remove(groupPosition);
Log.i("Prativa Removed ", "group" + groupPosition);
notifyDataSetChanged();
}
}
});
return convertView;
}
I have saved the states of hash map as follows
public static HashMap<Integer,Boolean> selectedGroupMembers = new HashMap<Integer, Boolean>();
Upvotes: 0
Views: 1096
Reputation: 374
Hence I found out the solution. The complete solution is integrated in the question itself as a final edit
Upvotes: 1