android - The setDefaultValue for ListPreference don't work using dynamic entries -


i have set lp.setdefaultvalue("2"), when run app, find 2th item isn't selected, why? thanks!

public class photopreference extends preferenceactivity{      @override      protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);              addpreferencesfromresource(r.xml.mypreference);              setcontentview(r.layout.photo_preference);              // adview resource , load request.             adview adview = (adview)this.findviewbyid(r.id.adviewpreference);             adview.loadad(new adrequest());              listpreference lp = (listpreference)findpreference("setlastfolder");                         charsequence[] entries = { "one", "two", "three" };             charsequence[] entryvalues = { "1", "2", "3" };              lp.setentries(entries);             lp.setentryvalues(entryvalues);              lp.setdefaultvalue("2");              }  }    <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"     android:key="apppreference"     android:summary="@string/preferencesummary"     android:title="@string/preference" >         <listpreference             android:key="setlastfolder"         android:dialogtitle="@string/selectgallery"         android:title="@string/selectgallery"         android:summary="@string/selectgallerysummary"                 android:layout="@layout/photopreference_layout"       />  </preferencescreen> 

try setvalueindex() instead of setdefaultvalue().

see this question

so if change...

lp.setdefaultvalue("2"); 

to...

lp.setvalueindex(int index); // pass appropriate index 

then should work.

updated
i hope defaultvalue locate index 1 when user have not choice!

for this, can check if lp null first , set defaultvalue...

if(lp.getvalue() == null) {    lp.setvalueindex(1); } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -