編程方式添加的偏好基本上是我想是這樣的:堅持的Android
- >我有一些設置,(當然)可以由用戶進行修改,在它是
「立方體號」 - >還有一個取決於此設置(移動)的設置
---->如果有一個立方體設置被禁用(這起作用)
---->如果有兩個立方體有兩個立方體運動和設置的選項啓用(這也適用)
---->如果有四個立方體需要爲運動添加一個選項,這裏說明我的問題:
我可以以編程方式更改ListPreference的價值添加此設置,但:
- >當用戶設置的附加價值「配對」
和
- >他(她)從設置移開時,設置被正確讀取
然而
→當用戶移回設置時,設置被設置爲列表的第一個元素(同步),而不是他先前做出的選擇
→設置(配對)被我通過撥打的SharedPreferences實例記住:
PreferenceManager.getDefaultSharedPreferences(context);
但是當移動到設置再次顯示設置的錯誤值(沒人願意)
如何堅持以編程方式增加值?
當然,原因是我在活動恢復時再次讀取preferences.xml,但我不知道如何在重新創建活動時堅持用戶所做的選擇。
這是我的代碼(這兩種方法也如此)
public class SettingsActivity extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("SA", "onCreate");
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
updateLists();
}
private void updateLists() {
Log.d("SA", "updateLists");
Preference numberOfCubesPref = findPreference("numberOfCubes");
Preference tupleTypePref = findPreference("tuple");
Preference movementTypePref = findPreference("movement_type");
Preference pictureDistributionPref = findPreference("distribution_of_pictures");
ListPreference numberOfCubesListPref = (ListPreference) numberOfCubesPref;
if(numberOfCubesListPref.getEntry() == null){
numberOfCubesListPref.setValueIndex(0);
}
numberOfCubesPref.setSummary(numberOfCubesListPref.getEntry());
ListPreference movementTypeListPref = (ListPreference) movementTypePref;
if(movementTypeListPref.getEntry() == null){
movementTypeListPref.setValueIndex(0);
}
if (numberOfCubesListPref.getEntry().equals("Four")) {
movementTypePref.setEnabled(true);
pictureDistributionPref.setEnabled(true);
CharSequence[] oldEntries = movementTypeListPref.getEntries();
if (oldEntries.length == 2) {
Log.d("SA","length is twoo");
CharSequence[] newEntries = new CharSequence[oldEntries.length + 1];
newEntries[0] = oldEntries[0];
newEntries[1] = "Paired";
newEntries[2] = oldEntries[1];
movementTypeListPref.setEntries(newEntries);
CharSequence[] oldEntryValues = movementTypeListPref
.getEntryValues();
CharSequence[] newEntryValues = new CharSequence[oldEntryValues.length + 1];
newEntryValues[0] = oldEntryValues[0];
newEntryValues[1] = "Paired";
newEntryValues[2] = oldEntryValues[1];
movementTypeListPref.setEntryValues(newEntryValues);
}
} else if (numberOfCubesListPref.getEntry().equals("Two")) {
movementTypePref.setEnabled(true);
pictureDistributionPref.setEnabled(true);
CharSequence[] oldEntries = movementTypeListPref.getEntries();
if (oldEntries.length == 3) {
CharSequence[] newEntries = new CharSequence[oldEntries.length - 1];
newEntries[0] = oldEntries[0];
newEntries[1] = oldEntries[2];
movementTypeListPref.setEntries(newEntries);
CharSequence[] oldEntryValues = movementTypeListPref
.getEntryValues();
CharSequence[] newEntryValues = new CharSequence[oldEntryValues.length - 1];
newEntryValues[0] = oldEntryValues[0];
newEntryValues[1] = oldEntryValues[2];
movementTypeListPref.setEntryValues(newEntryValues);
}
} else {
movementTypePref.setEnabled(false);
pictureDistributionPref.setEnabled(false);
}
ListPreference pictureDistributionListPref = (ListPreference) pictureDistributionPref;
ListPreference tupleTypeListPref = (ListPreference) tupleTypePref;
if(tupleTypeListPref.getEntry() == null){
tupleTypeListPref.setValueIndex(0);
}
CharSequence[] entries = pictureDistributionListPref.getEntries();
CharSequence target, replacement;
if (tupleTypeListPref.getEntry().equals("Two of the same kind")) {
target = "Triplet";
replacement = "Pair";
} else {
target = "Pair";
replacement = "Triplet";
}
for (int i = 0; i < entries.length; i++) {
entries[i] = ((String) entries[i]).replace(target, replacement);
}
pictureDistributionListPref.setEntries(entries);
if(pictureDistributionListPref.getEntry() == null){
pictureDistributionListPref.setValueIndex(0);
}
tupleTypePref.setSummary(tupleTypeListPref.getEntry());
movementTypePref.setSummary(movementTypeListPref.getEntry());
pictureDistributionPref.setSummary(pictureDistributionListPref.getEntry());
}
和我的preferences.xml(相關部分):
<ListPreference
android:dialogTitle="@string/choose_movement_type"
android:enabled="false"
android:entries="@array/movement_type_entries"
android:entryValues="@array/movement_type_values"
android:key="movement_type"
android:title="@string/movement_type" />
和strings.xml中:(相關片)
<string name="movement_type">Movement type</string>
<string name="choose_movement_type">How do you want to control the cubes?</string>
<string-array name="movement_type_entries">
<item>Synchronized</item>
<item>Independent</item>
</string-array>
<string-array name="movement_type_values">
<item>Synchronized</item>
<item>Independent</item>
</string-array>
該活動從以下其他活動中調用:
settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
SettingsActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
任何幫助/意見/建議歡迎:)
S.
這個類只是選擇/放置值與一個鍵引用,我還想添加新的鍵並將它們保存在設置中(沒有重新加載settings.xml) – Bamboomy
據我所知,你正在試圖做的事情是不可能的。因爲你試圖修改apk。 (xml文件是在製作應用程序時創建的,在編譯應用程序後無法修改。)您可以將xml文件保存到手機外部存儲,然後從此處讀取/修改它。初始讀取默認的xml首選項後,您應該重新定位自己的共享首選項。 –