Search code examples
androidpreferences

preferences - add value to end list pref android


I am trying to add an item to a list pref,using this:

void setupValues(ListPreference pref) {
    CharSequence[] entries= pref.getEntries();
    entries[entries.length]="l";
    pref.setEntries(entries);
}

But this is not working, although I am able to create a new CharSequence and set as EntryValues, like this:

void setupValues(ListPreference pref) {
    CharSequence[] entries= {"d","e"};
    pref.setEntries(entries);
}

So is there any solution?


Solution

  • Try following code. I didn't implemented but may solve your proflem.

    CharSequence[] entries = new String[pref.getEntries().length + 1];
    
    int i = 0;
    for (CharSequence cs : pref.getEntries()) {
        entries[i] = cs;
        i++;
    }
    
    entries[entries.length] = "l";
    pref.setEntries(entries);