Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableListView and checkboxes

Tags:

android

I'm writing simple filter in Android and want to use ExpandableListAdapter with check boxes.

I have no problem with creating list or checking checkbox. but I really don't know how to remember choices. After closing group and opening again or when I try to open different group checkboxes are changes.

I tried to reading on the net about that but I didn`t 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();                       // Setting Adapter Values to use on list
    list.setAdapter(listAdapter);           // Setting creating adapter to list
    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:
                //TODO After getting Tasks function

            }
            return false;
        }

    });

}

}

like image 707
Adrian Deja Avatar asked Nov 05 '22 03:11

Adrian Deja


1 Answers

It depends on how much you want to engineer it. A Map> would work. The Map takes a position and gives you a set of checked children. To determine if a child is checked or not, call Map.get() on group, and then see if the set contains the id of the child.

If you want to see if the second child of the third group is checked, you could do:

boolean isChecked = yourMap.get(3).contains(2);

To set a child checked:

yourMap.get(groupNum).add(childNum);

To set a child unchecked:

yourMap.get(groupNum).remove(childNum);

You would need to initialize the map to contain empty sets for every group to avoid NPEs, but thats easy to do when you first create the map. I'm sure you could come up with a cleaner way to do this, but its simple and it would work.

like image 117
Sam Judd Avatar answered Nov 09 '22 06:11

Sam Judd