我覺得我一直在尋找每一個沒有找到答案的地方。Android將項目添加到列表偏好程序化
我有一個列表偏好
<ListPreference
android:key="signedUpCompetetion"
android:entries="@array/listArray"
android:entryValues="@array/listValues"
/>
用下面的條目和entryValues:
<resources>
<string-array name="listArray">
<item></item>
<item></item>
<item></item>
</string-array>
<string-array name="listValues">
<item></item>
<item></item>
<item></item>
</string-array>
</resources>
所以說,在我的活動,我想的東西添加到列表中的一個。
下面是一個嘗試,但它似乎沒有工作:
this.appPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
this.prefEditor = appPrefs.edit();
ListPreference list = (ListPreference) getSharedPreferences("signedUpCompetetion", MODE_PRIVATE);
list.setEntries("This is a test");
任何人能告訴我如何添加到listpreference?
更新 通過它的外觀,這似乎工作
PreferenceScreen root = this.getPreferenceScreen();
ListPreference list = (ListPreference) root.findPreference("signedUpCompetetion");
CharSequence[] entries = { "One", "Two", "Three" };
CharSequence[] entryValues = { "1", "2", "3" };
list.setEntries(entries);
list.setEntryValues(entryValues);
然而,有1點的問題!如果我重新啓動應用程序列表不保留。這意味着列表是空的!
現在更新之後,目前還不清楚問題到底是什麼。你的意思是列表出現在首次發佈但不連續發佈?你如何管理你的活動生命週期? – ozbek
列表是否在第一次啓動時自動顯示?或者你期望用戶交互(例如,按下按鈕)? – ozbek
好吧讓我來解釋一下我的意思:我想要添加一個列表,我可以在這個列表中添加字符串在運行時使用這個列表的方式是,當應用程序關閉時它必須被壓制,以便下次用戶打開應用程序時,他將能夠看到完整列表 –