Android 共享首选项不会立即应用
我有一个活动列表,点击其中一个我会得到一个列表视图 在这里的列表视图中,我可以使用菜单按钮应用首选项。但此首选项不会立即应用于ListView 我必须返回并浏览活动的父列表,当我单击时,只有我得到了我想要的首选项。Android 共享首选项不会立即应用,android,android-layout,android-preferences,Android,Android Layout,Android Preferences,我有一个活动列表,点击其中一个我会得到一个列表视图 在这里的列表视图中,我可以使用菜单按钮应用首选项。但此首选项不会立即应用于ListView 我必须返回并浏览活动的父列表,当我单击时,只有我得到了我想要的首选项。 我们不能在应用此首选项/设置后立即应用它吗?您可以将共享首选项更改侦听器上的添加到您的活动中: 公共类MyList扩展ListActivity在共享首选项ChangeListener上实现{ 并定义onSharedPreferenceChange()方法以立即手动更改设置。您必须使用
我们不能在应用此首选项/设置后立即应用它吗?您可以将共享首选项更改侦听器上的
添加到您的活动中:
公共类MyList扩展ListActivity在共享首选项ChangeListener上实现{
并定义onSharedPreferenceChange()
方法以立即手动更改设置。您必须使用SharedReferences.RegisterOnSharedReferenceChangeListener(此)
也一样。但我应用的视图是通过游标适配器。当我将此游标放在共享首选项ChangeListener中时,它会给我一些构造函数错误“构造函数MyCountriesActivity.MySimpleCursorAdapter(新的共享引用.OnSharedPreferenceChangeListener(){},int,cursor,String[],int[])未定义”。我试图相应地调整MySimpleCursorAdapter中的构造函数,但我无法做到。解决方案是什么
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Implementation
int sort = prefs.getInt("sort_pref", -1); // -1 will be the result if no preference was set before
if(sort == 1)
sortOrder = "year";//sort on year
else if (sort == 2)
sortOrder = "country";//sort on country
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);
String[] from = new String[] { "year", "country" };
int[] to = new int[] { R.id.year, R.id.country };
SimpleCursorAdapter sca = new MySimpleCursorAdapter(this, R.layout.country_row,
c, from, to);
setListAdapter(sca);
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
}
class MySimpleCursorAdapter extends SimpleCursorAdapter{
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
@Override // Called when updating the ListView
public View getView(int position, View convertView, ViewGroup parent) {
/* Reuse super handling ==> A TextView from R.layout.list_item */
View v = super.getView(position,convertView,parent);
TextView tYear = (TextView) v.findViewById(R.id.year);
TextView tCountry = (TextView) v.findViewById(R.id.country);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean font_size = prefs.getBoolean("fontSize", false);
boolean italic_font = prefs.getBoolean("fontItalic", false);
String listpref = prefs.getString("bgColor", "#ffffff80");
//System.out.println(listpref);
tYear.setBackgroundColor(Color.parseColor(listpref));
tCountry.setBackgroundColor(Color.parseColor(listpref));
if (font_size){
tYear.setTextSize(25);
tCountry.setTextSize(25);
}
if (italic_font){
tYear.setTypeface(null, Typeface.ITALIC);
tCountry.setTypeface(null, Typeface.ITALIC);
}
return v;
}
}
但我正在应用的视图是通过自定义cursoradapter在onCreate中扩展cursoradapter的。我有一些想法,我必须在onCreate中的SharedPreferenceChangeListener上编写,但如何才能做到这一点。我的光标适配器在onCreate中。好吧,阅读错误消息。在这一行:SimpleCursorAdapter sca=new MySimpleCursorAdapter(this,R.layout.country_row,c,from,to);
您使用this
作为第一个参数创建一个新的MySimpleCursAdapter。“this”在这种情况下是侦听器,而您的构造函数不需要一个OnSharedPreferenceChangeListener
。我没有理解您的意思。我已经准备好了一个构造函数,除了这个OnSharedPreferenceChangeListener之外,我还将它用于其他用途。现在,当我尝试使用同一个mySimpleCorsAdapter时无法修复构造函数错误问题查看MySimpleCursorAdapter的构造函数。第一个参数用于上下文
。在我指出的行中,您将此
作为第一个参数。但是,此
不是上下文
对象。请尝试将其替换为获取*上下文()
methods.yes当我将第一个参数用作getContext()时谢谢。但我仍然有一个问题。首选项与复选框配合良好,但与listpreferences不配合。当我选择一个列表首选项时,它不起作用,然后复选框也不起作用。当我返回主列表并处理此列表时,首选项也会显示效果,但不会立即显示。f或者ListPreferences[String listpref=prefs.getString(“bgColor”,“#ffffffff80”);]也适用于我的数组[]和[]如果没有任何真正的代码,很难判断发生了什么。您可能想标记这个问题的答案,因为它涉及到自动设置prefs。至于您的新问题,您可能想将颜色存储为int
,比如0xff000000
,而不是字符串
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int sort = prefs.getInt("sort_pref", -1); // -1 will be the result if no preference was set before
if(sort == 1)
sortOrder = "year";//sort on year
else if (sort == 2)
sortOrder = "country";//sort on country
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);
String[] from = new String[] { "year", "country" };
int[] to = new int[] { R.id.year, R.id.country };
SimpleCursorAdapter sca = new MySimpleCursorAdapter(getBaseContext(), R.layout.country_row,
c, from, to);
setListAdapter(sca);
//System.out.println("sjfs");
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);