本文介绍了MultiSelectListPreference Android检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检索值,但不知何故我又得到了更多的值.这是我正在运行的代码.有人知道我在做什么错吗?我确实在很多地方搜索过,但是大多数都已经过时了.(我检查了项目2和3)只是为了确保我只想读出列表中选中的复选框,但我不知道0-1-2-123的来源.

pref_app.xml

 < MultiSelectListPreferenceandroid:title =类别"android:key ="rssfeeds"android:summary =列表以供选择"android:entries ="@ array/catos"android:entryValues ="@ array/catovalues"android:defaultValue ="@ array/catodefault"android:dialogTitle =类别"></MultiSelectListPreference> 

Strings.xml

 < string-array name ="catos">< item>电影</item>< item> Test</item>< item> TEst23</item></string-array>< string-array name ="catovalues"><项目>电影检查的</item>< item> test-checked</item>< item> test23-checked</item></string-array>< string-array name ="catodefault"><项目>电影默认值</item>< item> test-default</item>< item> TEST23-default</item></string-array> 

在主xml中返回代码

  SharedPreferences首选项= PreferenceManager.getDefaultSharedPreferences(getBaseContext());设置< String>selections = preferences.getStringSet("rssfeeds",null);String [] selected = selections.toArray(new String [] {});for(int i = 0; i< selected.length; i ++){System.out.println("\ ntest" + i +:" + selected [i]);} 

Logcat结果:

  01-15 14:15:49.016:I/System.out(4555):test0:test23已检查01-15 14:15:49.016:I/System.out(4555):test1:201-15 14:15:49.016:I/System.out(4555):test2:101-15 14:15:49.016:I/System.out(4555):test3:001-15 14:15:49.016:I/System.out(4555):test4:12301-15 14:15:49.016:I/System.out(4555):test5:经过测试 
解决方案

它仅存储已检查的内容.

您可以将值作为索引.

 < string-array name ="catos">< item>电影</item>< item> Test</item>< item> TEst23</item></string-array>< string-array name ="catovalues">< item> 0</item><项目> 1</item><项目> 2</item></string-array>0-电影=>已检查1-测试=>未检查2-TEst23 =>已检查 

在首选项文件中将仅索引0和2,并且您可以假定其他未标记.

因此,如果您分析 Set< String> ,则其大小将仅为2个 strings ,分别为0和2.

im trying to retrieve the values but somehow i get alot more values back.this is the code im running. Anyone knows what im doing wrong? i did search at alot of places but most is outdated. ( i checked item 2 and 3)Just to make sure i only want to read out the selected checkboxes of the list but i have no idea where the 0-1-2-123 come from.

pref_app.xml

    <MultiSelectListPreference
        android:title="Categories"
        android:key="rssfeeds"
        android:summary="List to choose from"
        android:entries="@array/catos"
        android:entryValues="@array/catovalues"
        android:defaultValue="@array/catodefault"
        android:dialogTitle="Categories"
        >
    </MultiSelectListPreference>

Strings.xml

<string-array name="catos">
    <item >Movies</item>
    <item >Test</item>
    <item >TEst23</item>
</string-array>
<string-array name="catovalues">
    <item >movies-checked</item>
    <item >test-checked</item>
    <item >test23-checked</item>
</string-array>
    <string-array name="catodefault">
    <item >movies-default</item>
    <item >test-default</item>
    <item >TEST23-default</item>
</string-array>

return code in main xml

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        Set<String> selections = preferences.getStringSet("rssfeeds", null);
        String[] selected= selections.toArray(new String[] {});
        for (int i = 0; i < selected.length ; i++){
            System.out.println("\ntest" + i +" : " + selected[i]);
        }

Logcat result:

01-15 14:15:49.016: I/System.out(4555): test0 : test23-checked
01-15 14:15:49.016: I/System.out(4555): test1 : 2
01-15 14:15:49.016: I/System.out(4555): test2 : 1
01-15 14:15:49.016: I/System.out(4555): test3 : 0
01-15 14:15:49.016: I/System.out(4555): test4 : 123
01-15 14:15:49.016: I/System.out(4555): test5 : test-checked
解决方案

It stores only the ones that are checked.

You can put values ​​as indices.

<string-array name="catos">
    <item >Movies</item>
    <item >Test</item>
    <item >TEst23</item>
</string-array>
<string-array name="catovalues">
    <item >0</item>
    <item >1</item>
    <item >2</item>
</string-array>

0 - Movies => Checked 
1 - Test   => Unchecked 
2 - TEst23 => Checked 

In the preferences file will only indexes 0 and 2, and you can assume that others are unmarked.

So if you analyze the Set <String>, its size will be only 2 strings with 0 and 2.

这篇关于MultiSelectListPreference Android检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:50