ExpandableListView和复选框

时间:2012-03-21 22:06:07

标签: android

我正在Android中编写简单的过滤器,并希望在复选框中使用ExpandableListAdapter

我创建列表或检查复选框没有问题。但我真的不知道如何记住选择。关闭组并再次打开或当我尝试打开不同的组时,复选框是更改。

我试图在网上阅读,但我没有找到如何解决我的问题。

这是我的代码:

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;
        }

    });

}

}

1 个答案:

答案 0 :(得分:2)

这取决于你想要设计多少。地图&gt;会工作。地图占据一个位置,并为您提供一组已检查的孩子。要确定是否检查了子项,请在组上调用Map.get(),然后查看该组是否包含子项的ID。

如果您想查看是否检查了第三组的第二个孩子,您可以这样做:

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

设置子项:

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

设置未选中的子项:

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

您需要初始化地图以包含每个组的空集以避免NPE,但这在您首次创建地图时很容易。我相信你可以想出一个更干净的方法来做到这一点,但它很简单,它会工作。