开发者

Shared preferences is not applied right away

I have a list of activities on clicking one of which i will get a ListView.

On this ListView here, I can 开发者_StackOverflowapply the preferences using a menu button. But this preferences is not applied right away to the ListView.

I have to go back and navigate my way through the parent list of activities and when i click then only I get the preferences that I want is applied. cant we get this preferences/settings applied right away after we apply it?


But the view I am applying is through a cursoradapter. When I put this cursor inside OnSharedPreferenceChangeListener it gives me some constructor error "The constructor MyCountriesActivity.MySimpleCursorAdapter(new SharedPreferences.OnSharedPreferenceChangeListener(){}, int, Cursor, String[], int[]) is undefined". I tried to adjust the constructor in MySimpleCursorAdapter accordingly but I am not being able to do that. What is the solution?

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


You can add OnSharedPreferenceChangeListener to your Activity:

public class MyList extends ListActivity implements OnSharedPreferenceChangeListener {

and define the onSharedPreferenceChange() method to manually alter settings immediately. You'll have to use SharedPreferences.registerOnSharedPreferenceChangeListener(this) as well.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜