0

我想评估 checkboxpreference,从这里尝试了一些方法,但没有奏效。我需要在 OnSharedPreferenceChangeListener 中获取值,而不是在 PreferenceActivity 中。它给出了如下错误: ava.lang.ClassCastException: android.app.SharedPreferencesImpl cannot be cast to android.support.v7.preference.CheckBoxPreference 有人可以解释这个问题吗?

sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                Log.d(MainActivity.this.getClass().getSimpleName(), "Einstellungen wurde geändert.");
                prefsChanged = true;
                if(key.equals("use_gps")) {
                    //TODO: CheckBox evaluate and LocationUpdate start or remove if it's not checked
CheckBoxPreference preference = (CheckBoxPreference) getSharedPreferences("use_gps", MODE_PRIVATE);
                 if(preference.isChecked()) {
                     requestLocationUpdates();
                     Log.d(getClass().getSimpleName(), "preference ischecked");
                 } else {
                     removeLocationUpdates();
                     Log.d(getClass().getSimpleName(), " preference isnotchecked");
                 }


                }

            }
        };

感谢所有帮助,解决方案在 MAinActivity onCreate() 中:

if(key.equals("use_gps")) {
                    //TODO: CheckBox auswerten udn ggfs. Standortbestimmung starten oder stoppen
                    boolean checkbox = sharedPreferences.getBoolean("use_gps", true);
                    if (checkbox == true){
                        Toast.makeText(MainActivity.this, "true", Toast.LENGTH_LONG).show();
                        requestLocationUpdates();
                    } else {
                        Toast.makeText(MainActivity.this, "false", Toast.LENGTH_LONG).show();
                        removeLocationUpdates();
                    }

                }

            }
4

1 回答 1

0

这应该有助于,而不是使用OnSharedPreferenceChangeListener(),registerOnSharedPreferenceChangeListener()将侦听器链接到 Preference 之外的任何偏好

非首选项 Activity 的代码片段,您希望在其中附加首选项的侦听器:

SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);

prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener()
{
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
       Log.v("CHANGE ", "YES");

       boolean use_gps_stat = sharedPreferences.getBoolean("use_gps", true);

       if(key == "use_gps")
       {
           Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
           if(use_gps_stat)
           {
               Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_LONG).show();

               requestLocationUpdates();
               Log.d(getClass().getSimpleName(), "preference ischecked");
           }
           else
           {
               Toast.makeText(getApplicationContext(), "Unchecked", Toast.LENGTH_LONG).show();

               removeLocationUpdates();
               Log.d(getClass().getSimpleName(), " preference isnotchecked");
           }
       }
    }
});

在您的偏好活动中,将此添加到覆盖onCreate

bindPreferenceSummaryToValue_CheckBox(findPreference("use_gps"));

所以它应该是这样的:

public static class MainPreferenceFragment extends PreferenceFragment 
{
    @Override
    public void onCreate(final Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);      
        bindPreferenceSummaryToValue_CheckBox(
                         findPreference("use_gps"));
    .
    .
    .

并在bindPreferenceSummaryToValue_CheckBox外面添加onCreate

private static void bindPreferenceSummaryToValue_CheckBox(Preference preference)
{
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.getContext())
                    .getBoolean(preference.getKey(), true));
}

sBindPreferenceSummaryToValueListener只需添加到扩展为的类AppCompatPreferenceActivity

private static Preference.OnPreferenceChangeListener
        sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener()
{
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue)
    {
        //Do something on prefernce change.
        return true;
    }
};

我最近尝试了这种代码结构并工作了,如果有任何疑问,请在评论中告诉我。

于 2019-06-17T18:11:59.433 回答