2017-07-17 45 views
0

我有一個應用程序,其中包含ArrayList中存儲在共享首選項中的項目,我想從共享首選項中刪除ArrayList中的特定項目。如何做到這一點如何從android中的sharedPreference中存儲的arraylist中刪除項目?

代碼: -

String jsonData = sharedPreference.getAppsArrayListData(); 
         Type type = new TypeToken<ArrayList<WhiteListModel>>() { 
         }.getType(); 
         whiteListStorage = gson.fromJson(jsonData, type); 

共享優先級代碼: -

private SharedPreferences pref; 
private SharedPreferences.Editor editor; 
// Context 
private Context _context; 

// Shared pref mode 
int PRIVATE_MODE = 0; 

// Sharedpref file name 
private static final String PREF_NAME = "pref"; 
private static final String NOTIFICATION = "appData"; 

public MySharedPreference(Context context) { 
    this._context = context; 
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
    editor = pref.edit(); 
} 

public void saveAppsArrayListData(String scoreString) { 
    editor.putString(NOTIFICATION, scoreString); 
    editor.commit(); 
} 

public String getAppsArrayListData() { 
    return pref.getString(NOTIFICATION, ""); 
} 

}

+0

只需從arrayList中刪除位置或字符串中的數據,並在sharedPreference中重置該值。 –

+0

請參考一些代碼snipshot – Niraj

回答

1

要刪除特定保存PREF然後用

SharedPreferences.Editor editor = settings.edit(); 
editor.remove("tag_to_delete"); 
editor.commit(); 

如果你想要t o從共享首選項中刪除ArrayList中的特定項目,然後 從list.remove(position)中刪除列表中的項目並再次保存

+0

@Niraj你解決了你的問題? –

相關問題