如何从android中的sharedPreference中存储的arraylist中删除项目?

时间:2017-07-17 06:45:41

标签: android arraylist sharedpreferences

我有一个应用程序,其中包含ArrayList中的项目,这些项目存储在共享首选项中,我想从共享首选项中的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, "");
}

}

1 个答案:

答案 0 :(得分:2)

要删除特定保存的首选项,请使用

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

如果要以共享首选项从ArrayList中删除特定项目 使用list.remove(position)从列表中删除项目并再次保存