Updating Default preferences

huangapple go评论76阅读模式
英文:

Updating Default preferences

问题

Here's the translation of the provided content:

我遇到了一个问题,我必须在运行主应用程序之前设置一系列的设置。这些设置是通过一个引导流程完成的,然而一旦进入主应用程序后,这些设置在主设置中没有反映出来。

在引导流程中,我进行了以下设置:

       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioId = checkedId;
                radioButton = getActivity().findViewById(radioId);
                String str = (String) radioButton.getText();
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext());
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(kDirection, str).apply();
                editor.commit();
            }
        });

我的设置偏好项:

    <PreferenceCategory app:title="方向">

        <ListPreference
            app:key="kDirectionSetting"
            app:entryValues="@array/directions"
            android:entries="@array/directions"
            app:useSimpleSummaryProvider="true"
            app:title="方向设置"
            />

    </PreferenceCategory>

如果我在打开首选项屏幕之前在设备文件浏览器中打开它们,这会在首选项中反映出来。然后,当打开主菜单并加载首选项屏幕时,字符串会被更改为我数组中的未设置默认值,每次都是相同的。

我如何让反映的更改第一次显示在我的设置中?

英文:

I have an issue, i have to set a whole host of settings prior to running my main app. These are done through an on-boarding process, however this is not being reflected in my main settings once in the main app.

During on-boarding i set:

       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioId = checkedId;
                radioButton = getActivity().findViewById(radioId);
                String str = (String) radioButton.getText();
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext());
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(kDirection, str).apply();
                editor.commit();
            }
        });

My Preference:

    &lt;PreferenceCategory app:title=&quot;Directions&quot;&gt;

        &lt;ListPreference
            app:key=&quot;kDirectionSetting&quot;
            app:entryValues=&quot;@array/directions&quot;
            android:entries=&quot;@array/directions&quot;
            app:useSimpleSummaryProvider=&quot;true&quot;
            app:title=&quot;Direction Preference&quot;
            /&gt;

    &lt;/PreferenceCategory&gt;

This is reflected in my preferences If i open them in device file explorer before i open the preferences screen. Then when the main menu is opened and the preference screen loaded the String is changed to an unset default value from my array, the same every time.

How do i get the reflected changes to show in my settings first time?

答案1

得分: 1

I think that if you have the same key for both your ListPreference and the initial Preference value that you stored, then you will get the ListPreference to edit the initial value. However, do this to set the default value to the ListPreference:

@Override
public void onCreate(Bundle savedInstanceState) {
    ListPreference kDirectionPref = (ListPreference) findPreference("kDirectionSetting");
    kDirectionPref.setDefaultValue(prefs.getString("kDirection"));
}

So basically, what I'm saying is that you should ensure that you use the same key to put values for your Preference, i.e. in the first piece of code, kDirection should be changed to "kDirectionSetting".

英文:

I think that if you have the same key for both your ListPreference and the initial Preference value that you stored, then you will get the ListPreference to edit the initial value. However, do this to set the default value to the ListPreference:

@Override
public void onCreate(Bundle savedInstanceState) {
    ListPreference kDirectionPref = (ListPreference) findPreference(&quot;kDirectionSetting&quot;);
    kDirectionPref.setDefaultValue(prefs.getString(&quot;kDirection&quot;));

So basically, what I'm saying is that you should ensure that you use the same key to put values for your Preference, i.e. in the first piece of code, kDirection should be changed to &quot;kDirectionSetting&quot;.

huangapple
  • 本文由 发表于 2020年8月13日 00:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63380858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定