Reputation: 543
I'm trying to create a ListPreference in Android. But instead of creating all in XML i want to add the Entries and EntriesValues in JAVA. I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference android:key="delay"
android:title="@string/settings_push_delay"
android:defaultValue="Default">
</ListPreference>
</PreferenceScreen>
Then i have this class, extending PreferenceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
final ListPreference listPreference = (ListPreference) findPreference("delay");
listPreference.setOnPreferenceClickListener(this);
}
public boolean onPreferenceClick(Preference preference) {
ListPreference lp = (ListPreference)preference;
String[] array={"1","2","3"};
CharSequence[] entries = array;
CharSequence[] entryValues = array;
lp.setEntries(entries);
lp.setDefaultValue("1");
lp.setEntryValues(entryValues);
return true;
}
This is just a test that i'm trying to do, so i can fully understand how to create Preferences dynamically.
Forgot to say that i've having exceptions while running this code:
04-14 00:47:10.432: E/AndroidRuntime(1330): FATAL EXCEPTION: main
04-14 00:47:10.432: E/AndroidRuntime(1330): java.lang.IllegalStateException: ListPreference requires an entries array and an entryValues array.
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.ListPreference.onPrepareDialogBuilder(ListPreference.java:232)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.DialogPreference.showDialog(DialogPreference.java:293)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.DialogPreference.onClick(DialogPreference.java:264)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.Preference.performClick(Preference.java:939)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:202)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Handler.handleCallback(Handler.java:605)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.os.Looper.loop(Looper.java:137)
04-14 00:47:10.432: E/AndroidRuntime(1330): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-14 00:47:10.432: E/AndroidRuntime(1330): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 00:47:10.432: E/AndroidRuntime(1330): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 00:47:10.432: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-14 00:47:10.432: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-14 00:47:10.432: E/AndroidRuntime(1330): at dalvik.system.NativeStart.main(Native Method)
My question is, how i create a ListPreference without defining the Entries and EntryValues on the XML. How can do that in JAVA file. So, how i solve this exceptions?
Thanks,
Upvotes: 3
Views: 3581
Reputation: 5122
You cannot be setting the entries
and entryValues
of the ListPreference
in an onClick
event when there are no entries
and entryValues
to begin with.
Instead do it in the onCreate
.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
final ListPreference listPreference = (ListPreference) findPreference("delay");
String[] array={"1","2","3"};
CharSequence[] entries = array;
CharSequence[] entryValues = array;
listPreference.setEntries(entries);
listPreference.setDefaultValue("1");
listPreference.setEntryValues(entryValues);
}
Then if you need set values dynamically in onClick listener, it should work okay.
Upvotes: 5