0

我想在檢查切換按鈕時保存兩個字符串值,並從另一個Fragment檢索它。這是我所做的按鈕OnClickListener,但它不工作:如何使用SharedPreference存儲字符串數組?

holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
    @Override 
    public void onCheckedChanged(CompoundButton favButton, boolean isChecked){ 
     if (isChecked) 
      favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(),R.mipmap.ic_launcher)); 
     PreferenceManager.getDefaultSharedPreferences(context).edit().putString("PRODUCT_APP", "Product_Favorite").commit(); 

     else 
      favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_cart)); 
    } 
}); 

這是我SharedPreference

public class SharedPreference { 

    public static final String PREFS_NAME = "PRODUCT_APP"; 
    public static final String FAVORITES = "Product_Favorite"; 

    public SharedPreference() { 
     super(); 
    } 

    // This four methods are used for maintaining favorites. 
    public void saveFavorites(Context context, List<CardItemModel> favorites) { 
     SharedPreferences settings; 
     SharedPreferences.Editor editor; 

     settings = context.getSharedPreferences(PREFS_NAME, 
       Context.MODE_PRIVATE); 
     editor = settings.edit(); 

     Gson gson = new Gson(); 
     String jsonFavorites = gson.toJson(favorites); 

     editor.putString(FAVORITES, jsonFavorites); 

     editor.commit(); 
    } 

    public void addFavorite(Context context, CardItemModel product) { 
     List<CardItemModel> favorites = getFavorites(context); 
     if (favorites == null) 
      favorites = new ArrayList<CardItemModel>(); 
     favorites.add(product); 
     saveFavorites(context, favorites); 
    } 

    public void removeFavorite(Context context, CardItemModel product) { 
     ArrayList<CardItemModel> favorites = getFavorites(context); 
     if (favorites != null) { 
      favorites.remove(product); 
      saveFavorites(context, favorites); 
     } 
    } 

    public ArrayList<CardItemModel> getFavorites(Context context) { 
     SharedPreferences settings; 
     List<CardItemModel> favorites; 

     settings = context.getSharedPreferences(PREFS_NAME, 
       Context.MODE_PRIVATE); 

     if (settings.contains(FAVORITES)) { 
      String jsonFavorites = settings.getString(FAVORITES, null); 
      Gson gson = new Gson(); 
      CardItemModel[] favoriteItems = gson.fromJson(jsonFavorites, 
        CardItemModel[].class); 

      favorites = Arrays.asList(favoriteItems); 
      favorites = new ArrayList<CardItemModel>(favorites); 
     } else 
      return null; 

     return (ArrayList<CardItemModel>) favorites; 
    } 
} 
+0

此鏈接可能很有用:http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Koorosh

+0

你的'setOnCheckedChangeListener'不應該工作。沒有相應的最後一部分。請先修復您的代碼,然後再次發佈代碼。它應該給你一個編譯錯誤。 –

回答

2

讓我們來解決這個問題^^。 您可以通過在單個字符串中添加多個收藏夾來保存多個收藏夾,每個收藏夾項以逗號分隔。然後您可以使用convertStringToArray方法將其轉換爲字符串數組。這裏是完整的源代碼。 使用MyUtility方法保存多個收藏夾項目。

MyUtility.addFavoriteItem(this, "Sports"); 
     MyUtility.addFavoriteItem(this, "Entertainment"); 

得到所有收藏的字符串數組保存

String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"}; 

在單獨的實用工具類

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

}

保存這些方法如果你有添加額外的收藏夾。然後從SharedPreference得到最喜歡的字符串,並附加逗號+最喜歡的項目,並將其保存回SharedPreference。 *您可以使用任何其他字符串作爲分隔符而不是逗號。