1

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

1
  • the square bracket notation only works on array Commented Mar 9, 2013 at 18:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.