共享偏好不会立即应用

时间:2011-10-01 22:29:20

标签: android android-layout android-preferences

我有一个活动列表,点击其中一个我将获得ListView。

在此ListView上,我可以使用菜单按钮应用首选项。但是此偏好不会立即应用于ListView。

我必须返回并浏览父级活动列表,当我点击时,我只会获得我想要的首选项。 我们申请后,我们是否立即应用这些偏好/设置?

3 个答案:

答案 0 :(得分:2)

但我申请的观点是通过一个cursoradapter。当我把这个光标放在OnSharedPreferenceChangeListener中时,它给了我一些构造函数错误“构造函数MyCountriesActivity.MySimpleCursorAdapter(new SharedPreferences.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;       
    }
}

答案 1 :(得分:0)

您可以将OnSharedPreferenceChangeListener添加到您的活动中:

public class MyList extends ListActivity implements OnSharedPreferenceChangeListener {

并定义onSharedPreferenceChange()方法以立即手动更改设置。您还必须使用SharedPreferences.registerOnSharedPreferenceChangeListener(this)

答案 2 :(得分:0)

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