我想评估checkboxpreference,从这里尝试了一些方法,但没有一个奏效。我需要在OnSharedPreferenceChangeListener中获取值,而不必在PreferenceActivity中获取。它给出了如下错误: ava.lang.ClassCastException:android.app.SharedPreferencesImpl无法转换为android.support.v7.preference.CheckBoxPreference 有人可以解释这个问题吗?
db-ip.com
感谢所有帮助,解决方案在MAinActivity onCreate()中:
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");
}
}
}
};
答案 0 :(得分:0)
这应该有帮助,而不是使用OnSharedPreferenceChangeListener()
,而是使用registerOnSharedPreferenceChangeListener()
将侦听器链接到“首选项”和
非首选项活动的代码段,您要将监听器附加到首选项:
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;
}
};
我最近尝试了这种代码结构,并且工作正常,如果有任何疑问,请在注释中告知我。