I am writing a simple filter in Android and want to use ExpandableListAdapterwith checkboxes.
I have no problem creating a list or checking a checkbox. but I really don’t know how to remember the choice. After closing a group and opening again or when I try to open different group flags, these are changes.
I tried to read about it on the net, but I did not find how to solve my problem.
Here is my code:
public class TasksFilterDialog extends Dialog{
private static final String TAG = "Filter Dialog";
private static final String ChildID = "ChildID";
private static final String GroupID = "GroupID";
private boolean[] statusCheck;
ExpandableListView list;
SimpleExpandableListAdapter listAdapter;
public TasksFilterDialog(Context context) {
super(context);
statusCheck = new boolean[Tasks.Status.values().length];
for(int i=0; i<statusCheck.length; i++)
statusCheck[i]=true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "Creating dialog");
this.setContentView(R.layout.tasks_filter);
list = (ExpandableListView)findViewById(R.id.filter_list);
setListAdapter();
list.setAdapter(listAdapter);
setOnChildClickListeners();
Log.d(TAG, "Creating dialog successfull");
}
private void setListAdapter() {
Log.d(TAG, "Setting list adapter");
listAdapter = new SimpleExpandableListAdapter(this.getContext(),
getGroups(),
R.layout.tasks_filter_groups_layout,
new String[] {GroupID},
new int[] {R.id.filter_group_text},
getChilds(),
R.layout.tasks_filter_simple_element,
new String[] {ChildID},
new int[] {R.id.filter_simple_element_text});
Log.d(TAG, "Setting list adapter successfull");
}
private List<HashMap<String, String>> getGroups() {
Log.d(TAG, "Adding groups values");
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> statusMap = new HashMap<String, String>();
statusMap.put(GroupID, "Status");
list.add(statusMap);
HashMap<String, String> usersMap = new HashMap<String, String>();
usersMap.put(GroupID, "Users");
list.add(usersMap);
Log.d(TAG, "Adding groups values successfull");
return list;
}
private List<List<HashMap<String, String>>> getChilds() {
Log.d(TAG, "Adding childs values");
List<List<HashMap<String, String>>> list = new ArrayList<List<HashMap<String, String>>>();
List<HashMap<String, String>> secList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
Tasks.Status status[] = Tasks.Status.values();
for(int i=0; i<status.length; i++) {
Log.d(TAG, "Adding child" + status[i].toString());
map = new HashMap<String, String>();
map.put(ChildID, status[i].toString());
secList.add(map);
}
list.add(secList);
list.add(secList);
Log.d(TAG, "Adding childs values succesfull");
return list;
}
private void setOnChildClickListeners() {
list.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView list, View v,
int group, int id, long arg4) {
CheckBox cb = (CheckBox) v.findViewById(R.id.filter_simple_element_check);
cb.toggle();
switch(group){
case 0:
if(statusCheck[id])
statusCheck[id]=false;
else
statusCheck[id]=true;
break;
case 1:
}
return false;
}
});
}
}