-1
public abstract class CartSharedPrefrences {
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;
}
}
我上面的解決方案嘗試了,而調試它的數據保存到共享 的喜好,但是當我試圖檢索共享偏好它 將返回空數據,同時增加了數據共享偏好被檢索 的方法之前檢查是否在共享偏好提供或 沒有任何數據,當時該方法將其返回檢查目的的數據,但 而從另一個活動則返回null調用。誰能幫我? 如何從中得到完美的價值。提前致謝。SharedPreferences保存多於一個的值返回null而retriving
'但同時從另一個活動調用它返回null。 '... *公共SharedPreferences的getPreferences(INT模式) 在API級別1 檢索SharedPreferences對象訪問是**私人這項活動的喜好。*** – Selvin
我有試圖http://stackoverflow.com/a/9054246/5324829解決方案,但其空 – gaurang