2

Why its not work in Code ? in XML its ok, but not in Code.

String [] testValues = {"a" , "b", "c"};
ListPreference test = (ListPreference)findPreference(KEY);
test.setEntries(testValues);
test.setEntryValues(testValues);
test.setDefaultValue(testValue[0]);
Jim
  • 8,874
  • 16
  • 68
  • 125
  • I'm talking about setDefaultValue....In XML its ok, but here in code its not...Why ? – Jim Mar 03 '11 at 09:59
  • 1
    This just in - you can accept answers by clicking the little check mark next to the answer given to one of your questions. – Codemonkey Mar 03 '11 at 10:07
  • http://stackoverflow.com/questions/5197228/how-to-set-the-default-value-of-a-listpreference/17509197#17509197 Could you see please, here is my answer. – Md. Naushad Alam Jul 07 '13 at 04:18

3 Answers3

2

How to set the Default Value of a ListPreference

Community
  • 1
  • 1
Travis
  • 2,170
  • 1
  • 19
  • 21
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Ali Bdeir Nov 02 '16 at 18:06
1

you can also try test.setValueIndex(0);

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
ungalcrys
  • 5,242
  • 2
  • 39
  • 23
-1

Instead of using setDefaultValue on ListPreference, you may use setValue to change the value. The complete code would be:

String [] testValues = {"a" , "b", "c"};
ListPreference test = (ListPreference)findPreference(KEY);
test.setEntries(testValues);
test.setEntryValues(testValues);
test.setValue(testValue[0]);
VCD
  • 889
  • 10
  • 27