質問

im using MultiSelectListPreference and the values save on array..

How can read??

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    Set<String> a = pref.getStringSet("tabs", null);

    for ( int i = 0; i < a.size(); i++) {
        Log.d("salida", a[i]);
    }

i get this error: The type of the expression must be an array type but it resolved to Set

役に立ちましたか?

解決

You want to use the Set, and since it isn't an array, the square brackets ([])are cannot be used to access indexes.

To easily read the values from the Set,use the enhanced for loop:

for (String str: a){
  Log.d("salida", str);
}

If you want to remove items from that Set as you loop through, you will have to use an Iterator, as shown in this answer.

Alternatively, if you want an array, you can use Set#toArray():

String [] prefStrings = a.toArray(new String[a.size()]);

Then you can use the square brackets (prefStrings[position]) to access an index.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top