简体   繁体   中英

Use SharedPreferences to store more than one “favorite”

I am making a quotes application, and I am using shared prefrences to store a users favorite quotes. I know I can use SQL but I am sticking with shared prefrences for the time being. This is how I am saving a favorite quote:

if(view.getId()==R.id.favoritesBtn){
        String stringData = quote.getText().toString();


        SharedPreferences.Editor editor = someData.edit();
        editor.putString("quote_string", stringData);
        editor.commit();

    }
    if(view.getId()==R.id.button){
        someData=getSharedPreferences(filename,0);
        String dataReturned = someData.getString("quote_string", "Couldn't Load Data");
        dataResults.setText(dataReturned);

    }

dataResults is of course the textview where the saved quote is displayed. Now, my question comes when I try to save another quote. The first saved quote is overwritten, and each time the user only sees his last "saved" quote. How can I make it so that the user can see all quotes he pressed the "favoritesBtn" on?

In your example you are only saving the most recent quote. You can use putStringSet() so save a Set of Strings in SharedPreferences. Another possible way would be to store the Quotes in a file if you need to preserve the order.

First thing is first: This is really a job for an sql database (especially since you can save multiple values)

Now to actually answer your question:

You have a couple options here.

You can use putStringSet() to put a full set of quotes in the preference

You can iterate over all shared preferences

Save your favorite "quote_string" as a delimited format

String quotes = someData.getString("quote_string", "");
quotes = quotes + "|" + <new quote>;
editor.put("quote_string", quotes);
//more code
String dataReturned = someData.getString("quote_string", "");
String[] separatedQuotes = dataReturned.split("|");

Save a preference as well pointing to quote preference names:

String quoteNames = someData.getString("quote_names", "");
if (quoteNames.length() > 0)
    quoteNames += "," + <quote_name>
quoteNames = "quote" + quoteNames.split("|").length;
editor.put("quote_names", quoteNames);
//more code
String dataReturned = someData.getString("quote_names", "");
String[] separatedQuoteNames = dataReturned.split("|");
for (String separatedQuoteName: separatedQuoteNames) {
    String quote = someData.getString(separatedQuoteName, "No Data:);
}

Obviously depending on your goal some options might be better than others.

Use putStringSet() for that:

Set<String> quotes = someData.getStringSet("quote_string", new HashSet<String>());
quotes.add(stringData);
SharedPreferences.Editor editor = someData.edit();
editor.clear();
editor.putStringSet("quote_string", quotes);
editor.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM