开发者

How to go about preferences

I have a list view displaying data like:

2010 Singapore 2009 India 2011 Sweden 2010 Germany

Now when I click menu button, I have two items in it which let me sort this list according to year or by country. The sorting is working fine.

What I want to do now is, when I click any of two option items it should along with sorting the list accordingly should now also save the way how it was sorted in preferences. So the next time when I open the app, it should be sorted exactly as stored in preferences. What is the approach I should follow here. I mean how can I store that value how I sorted last time in my preferences. I tried looking into some tutorials but didnt get the idea completely

Actually I just want to save last sorting order in the preferences. Next time when user starts the application, the countries list should be sorted in the same way as it was sorted when user closed app last time.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, SORT_BY_YEAR, Menu.NONE, "Sort by Year")
            .setAlphabeticShortcut('y');
    menu.add(Menu.NONE, SORT_BY_COUNTRY, Menu.NONE, "Sort by Country")
            .setAlphabeticShortcut('c');

    return(super.onCreateOptionsMenu(menu));
}

/* (non-Javadoc)
 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String[] from = new String[] { "year", "country" };
    int[] to = new int[] { R.id.year, R.id.country };       

    switch (item.getItemId()) {
        case SORT_BY_YEAR:
            Cursor cYear = getContentResolver().query(CONTENT_URI, null, null, null, "year");       
            SimpleCursorAdapter scaYear = new SimpleCursorAdapter(this, R.layout.country_row,cYear, from, to);      
            setListAdapter(scaYear);
            sortOrder = "year";
            return(true);

        case SORT_BY_COUNTRY:
            Cursor cCountry = getConte开发者_JAVA技巧ntResolver().query(CONTENT_URI, null, null, null, "country");     
            SimpleCursorAdapter scaCountry = new SimpleCursorAdapter(this, R.layout.country_row,cCountry, from, to);        
            setListAdapter(scaCountry);
            sortOrder = "country";
            return(true);
    }

    return(super.onOptionsItemSelected(item));
}


Have you looked into using SharedPreferences to store the preferred sort order?

When the user selects a sort order you store that in a shared preference, for example

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt("sort_pref", 1).commit(); // 1 = sort on year

Next time when you open the app you can read the preference value

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int sort = prefs.getInt("sort_pref", -1); // -1 will be the result if no preference was set before
if(sort == 1)
    ///sort on year
else if (sort == 2)
    ///sort on country


look at getSharedPreferences() in your Activity to get hold of a copy of SharedPreferences. You can set values into it onClose() and then pick it back up again when your app restarts in onCreate()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜