- >我有一些设置(当然)可以由用户修改,其中有多个立方体'
- >还有一个设置取决于此设置(移动)
---->如果有一个立方体,则禁用该设置(这有效)
---->如果有两个立方体,则有两个移动选项,并且启用该设置(这也有效)
---->如果有四个立方体需要为运动添加一个选择,这就解决了我的问题:
我可以通过编程方式更改ListPreference的值以添加此设置,但是:
- >当用户设置附加值"配对"
和
- > (s)他离开设置,正确读取设置
然而
- >当用户移回设置时,设置被设置为列表的第一个元素(同步),而不是他之前做出的选择
- >设置(配对)由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上。