arraylist
,
MatchingArrayList
,从我的服务器:
try {
JSONArray Object = new JSONArray(MatchingAsString);
//for every object in the Array
for (int x = 0; x < Object.length(); x++) {
final JSONObject obj = Object.getJSONObject(x);
MatchingArrayList.add(obj.getString("usernameMatch"));
}
[+111, +222]
数组表
到
sharedpreferences
SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();
Gson gsonMAL = new Gson();
String jsonMAL = gsonMAL.toJson(MatchingArrayList);
editorMAL.putString("MatchingArrayList", jsonMAL);
editorMAL.commit();
所以下一次使用应用程序时,如果
匹配ArrayList
[+111, +222, +333]
,例如,它将覆盖上次使用的
数组表
属于
[+111, +222]
匹配ArrayList
null
或者是空的。这个
不更新。我试过:
if (MatchingArrayList == null) {
SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();
editorMAL.putString("MatchingArrayList", null);
editorMAL.commit();
} else {
//save MatchingArrayList into sharedpreferences so we can use it elsewhere
SharedPreferences sharedPreferencesMAL = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorMAL = sharedPreferencesMAL.edit();
Gson gsonMAL = new Gson();
String jsonMAL = gsonMAL.toJson(MatchingArrayList);
editorMAL.putString("MatchingArrayList", jsonMAL);
editorMAL.commit();
}
But still the last arraylist is being used. How can I fix this?