English 中文(简体)
共同的偏好在适用上并不适用。
原标题:Shared preferences is not applied right away

我有一份点击活动清单,其中一份清单将收发。

On this ListView here, I can apply 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 {

并界定了<条码>共享参比()方法,以便立即人工改变环境。 页: 1

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签