title: Create ListPreferences in Android
body: <p>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:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" &gt;
      &lt;ListPreference android:key="delay"
          android:title="@string/settings_push_delay"
          android:defaultValue="Default"&gt;
      &lt;/ListPreference&gt;
  
  &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p>Then i have this class, extending PreferenceActivity:</p>
  
  <pre><code>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;
  
      }
  </code></pre>
  
  <p>This is just a test that i'm trying to do, so i can fully understand how to create Preferences dynamically.</p>
  
  <p>Forgot to say that i've having exceptions while running this code:</p>
  
  <pre><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)
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks,</p>