ExpandableListView和复选框 [英] ExpandableListView and checkboxes

查看:78
本文介绍了ExpandableListView和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试着阅读有关网上,但我发现didn`t如何解决我的问题。

I tried to reading on the net about that but I didn`t find how to solve my problem.

下面是我的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;
        }

    });

}

}

推荐答案

这取决于你想要多少工程师吧。一个地图>会工作。该地图需要一个位置,并为您提供一组检查孩子。或确定如果一个孩子被选中,则调用Map.get()上的组,然后看看是否集包含孩子的id。

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

要设置一个孩子检查:

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

要设置一个孩子选中:

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

您需要初始化地图包含空集每组避免NPE上,但那是很容易做到当你第一次创建地图。我相信你能想出一个更清洁的方式做到这一点,但它的简单,它会工作。

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.

这篇关于ExpandableListView和复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆