Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>You can save multiple favorites</strong> in a single preference by adding numerous favorites in a single string, each favorite item separated by comma. Then you can use <code>convertStringToArray</code> method to convert it into String Array. Here is the full source code.<br /> Use MyUtility Methods to save multiple favorite items.</p> <pre><code> MyUtility.addFavoriteItem(this, "Sports"); MyUtility.addFavoriteItem(this, "Entertainment"); </code></pre> <p>get String array of all favorites saved</p> <pre><code>String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"}; </code></pre> <p>Save these methods in separate Utility class<br /></p> <pre><code> public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shared Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return convertStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = sharedPreferences.getString(key, defaultValue); return temp; } private static String[] convertStringToArray(String str){ String[] arr = str.split(","); return arr; } } </code></pre> <p>If you have to <strong>add extra favorites</strong>. Then get favorite string from <code>SharedPreference</code> and append comma+favorite item and save it back into <code>SharedPreference</code>.<br /> * You can use any other string for separator instead of comma.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload