我如何能实现多选择列表preference一个“全选”选项? [英] How can I implement a 'Select all' option for Multi Select List Preference?

查看:666
本文介绍了我如何能实现多选择列表preference一个“全选”选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有什么办法可以选择/到code检查多选列表preference的所有框。

I want to know if there is any way to be able to select/check all boxes of the Multi-select List Preference through code.

这是为preference我的XML文件。

This is my XML file for the preference.


   

<MultiSelectListPreference
    android:entries="@array/list"
    android:entryValues="@array/lValues"
    android:key="list"
    android:summary="This is a list to choose from"
    android:title="Teams to Follow" />

</PreferenceScreen>

而这些数组:


<string-array name="list">
    <item>All Teams</item>
    <item>Team1</item>
    <item>Team2</item>
    <item>Team3</item>
    <item>Team4</item>
</string-array>
<string-array name="lValues">
    <item>All</item>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
</string-array>

现在,我的列表中的第一个项目将是所有的球队:我想让它使得用户选择所有参赛队的那一刻,所有的团队名称旁应有的支票给他们。

Now, the first item of my list will be 'All Teams' I want to make it such that the moment a user selects All Teams, all the team names should have a check next to them.

推荐答案

下面是我用,(注意:您将需要更改 adapter.getCount() array.length 您已经创建了从资源阵列):

Here's what I used, (nb. you will need to change adapter.getCount() to array.length where you have created your array from resource):

// create the dialog
final AlertDialog dialog = new AlertDialog.Builder(getActivity());
// set handlers for button presses etc.

// ....

// Set the click listener specifically for the 0th element
final ListView lv = dialog.getListView();
lv.setItemsCanFocus(true);
lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long viewid) {
        System.out.println("clicked" + position);
        CheckedTextView textView = (CheckedTextView) view;
        if(position == 0) {
             if(textView.isChecked()) {
                 SparseBooleanArray checkedPositions = lv.getCheckedItemPositions();
                 for (int i = 1; i < adapter.getCount(); i++) {
                    if(!checkedPositions.get(i)) {
                        lv.setItemChecked(i, true);
                    }
                 }
             } else {
                 for(int i = 1; i < adapter.getCount(); i++) {
                     lv.setItemChecked(i, false);
                 }
             }
        } else {
           // handle clicks for non-top element
        }
    }
});

这篇关于我如何能实现多选择列表preference一个“全选”选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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