简体   繁体   English

在Android中持久地以编程方式添加首选项

[英]Persisting programmatically added preferences in android

basically what I want is this: 基本上我想要的是:

-> I have some settings that (of course) can be modified by the user, on of it is the 'number of cubes' ->我有一些设置(当然)可以由用户修改,其中一个是“多维数据集数量”
-> There is an other setting which depends on this setting (movement) ->还有其他设置取决于此设置(运动)
----> if there is one cube the setting is disabled (this works) ---->如果有一个多维数据集,则该设置被禁用(有效)
----> if there are two cubes there are two options for movement and the setting is enabled (this works too) ---->如果有两个多维数据集,则有两个移动选项,并且启用了设置(这也可行)
----> if there are four cubes there needs to be a choice added for movement and here lays my problem: ---->如果有四个立方体,则需要为移动添加一个选择,这就是我的问题:

I can programmatically change the value of the ListPreference to add this setting but: 我可以以编程方式更改ListPreference的值以添加此设置,但是:
-> when the user sets the added value "paired" ->当用户将附加值设置为“成对”时
and
-> (s)he moves away from the settings, the setting is read correctly ->(s)他离开设置,正确读取了设置
however 然而
-> when the users moves back to the settings the setting is set to the first element of the list(synchronized), not being the choice (s)he made earlier ->当用户返回到设置时,该设置将设置为列表的第一个元素(已同步),而不是他之前做出的选择
-> the setting (paired) is remembered by the SharedPreferences instance which I get by calling: ->我通过调用以下方法获得的SharedPreferences实例会记住设置(配对):

    PreferenceManager.getDefaultSharedPreferences(context);

but when moving to the settings again shows the wrong value for the setting (which nobody wants) 但是当再次移动到设置时,会显示错误的设置值(没人想要)

How do I persist a programmatically added value? 如何坚持以编程方式增加价值?

Of course the reason is that I read the preferences.xml again when the Activity is resumed, but I don't know how to persist the choice made by the user when the Activity is recreated. 当然,原因是当恢复活动时我再次读取了preferences.xml,但是我不知道如何在重新创建活动时保持用户所做的选择。

This is my code: (the two methods that matter) 这是我的代码:(这两个方法很重要)

    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());
}

and my preferences.xml (relevant piece): 和我的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" />

and strings.xml: (relevant piece) 和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>

The activity is called from within an other activity like this: 该活动是从其他活动中调用的,如下所示:

            settings.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent myIntent = new Intent(MainActivity.this,
                    SettingsActivity.class);
            MainActivity.this.startActivity(myIntent);
        }
    });

any help/comments/tips are welcome :) 欢迎任何帮助/评论/提示:)

S. S.

it looks like you are modifying the preference temporary. 看来您正在修改临时偏好。 to save the prefrences and be sure it remains the same, use sharedPreferencesTurbo 要保存首选项并确保它保持不变,请使用sharedPreferencesTurbo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:编辑以编程方式添加的视图 - Android: Edit views added programmatically 将布局首选项应用于 android 无线电组中动态添加的按钮 - applying layout preferences to dynamically added buttons in android radiogroup Android以编程方式添加的ListPreference缺少标题 - Android programmatically added ListPreference has missing title Android以编程方式添加了视图重复索引 - Android Programmatically added view repeating index Android以编程方式添加的按钮具有匹配的父级宽度和高度 - Android programmatically added button has match parent width and height 无法在Android中显示ScrollView和textView(以编程方式添加) - unable to make the ScrollView and textView (added programmatically) visible in Android Android:在 styles.xml 中指定的填充未显示在以编程方式添加的 Z622DC6E7034DB10F1421FCBDCCBD - Android: Padding specified in styles.xml not showing in programmatically added TextView? Android Studio 约束集未为以编程方式添加的视图设置约束 - Android Studio constraint set not setting constraints for programmatically added view 领域未在Android上持久化交易 - Realm not persisting transaction on Android 在Android中保留Parcelable对象 - Persisting a Parcelable object in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM