检索hashmap数据SharedPreferences

我用HashMap来存储我使用API​​收到的数据。之后,我使用SharedPreferences来存储接收到的HashMap数据。存储部分完成。我可以看到我想用SharedPreferences存储的记录数。检索hashmap数据SharedPreferences

下面是代码来保存数据:

if (result != null && result.length() > 0) 

{

for (int j = 0; j < result.length(); j++)

{

JSONObject resultObj = result.getJSONObject(j);

String label_id = resultObj.getString("label_id");

String arabic = resultObj.getString("arabic");

String english = resultObj.getString("english");

String key = resultObj.getString("key");

//Create a new model and set the received value

LabelModel labelModel = new LabelModel();

labelModel.setLabelId(label_id);

labelModel.setArabic(arabic);

labelModel.setEnglish(english);

labelModel.setKey(key);

int label = Integer.parseInt(label_id);

//Put the value

map.put(label, labelModel);

}

}

//With the below line, I stored the hashMap data using SharedPreferences

Pref.setValue(mActivity, AppPrefrences.PREF_LABELS, map);

在上述步骤之后,我也跟着这组代码设置和得到从SharePreferences的值,这是我存储在应用程序中使用SharedPreferences 。 对于这一点,我用这个下面的代码:

public static String PREF_LABELS ="labels"; 

public static void setValue(@NonNull Context context, String key, Object obj) {

Pref.openPref(context);

Editor prefsPrivateEditor = Pref.sharedPreferences.edit();

prefsPrivateEditor.putString(key, String.valueOf(obj));

prefsPrivateEditor.commit();

prefsPrivateEditor = null;

Pref.sharedPreferences = null;

}

@Nullable

public static String getValue(@NonNull Context context, String key, Object obj) {

Pref.openPref(context);

String result = Pref.sharedPreferences.getString(key, String.valueOf(obj));

Pref.sharedPreferences = null;

return result;

}

现在,我想找回我存储在SharedPreferences的数据。 这里是我用来检索数据的代码:当调试应用程序

String labels = Pref.getValue(mActivity, AppPrefrences.PREF_LABELS, ""); 

,我得到标签以下格式的值。我收到的记录数量相同。

格式是这样的:

{572=com.*****[email protected], 598=com.*****[email protected], 590=com.*****[email protected], 103=com.*****..........} 

我怎样才能得到从这种格式的每个数据?

回答:

有一个在SharedPreferences为HashMap不支持。所以,你不能通过直接将它转换为字符串来保存HashMap,但是你可以将它转换为JSON字符串。在这种情况下,您可以使用google-gson。事情是这样的:

首先,包括依赖性:

compile 'com.google.code.gson:gson:2.8.2' 

从HashMap对象保存到偏好:

Editor prefsEditor = mPrefs.edit(); 

Gson gson = new Gson();

String json = gson.toJson(map);

prefsEditor.putString("YourHashMap", json);

prefsEditor.commit();

从偏好获取HashMap对象:

Gson gson = new Gson(); 

String json = mPrefs.getString("YourHashMap", "");

HashMap map = gson.fromJson(json, HashMap.class);

回答:

你得到{572=com.*****[email protected], 598=com.*****[email protected], 590=com.*****[email protected], 103=com.*****..........}这是因为对象默认toString()方法使用hashcode

将复杂对象存储在SharedPreference不推荐使用。 SharedPreference不支持HashMaplink。

对于存储简单对象,您必须将对象转换为字符串使用toString()方法。 您可以使用Gson将对象转换为字符串,这将是一个简单的解决方案。

您还可以使用ObjectOutputStream将其写入到内部存储器check this link

回答:

你应该将字符串转换为hashmap对象这是一个解决方案。如果你想使它更通用,你可以使用我们的StringUtils库。

String value = "{first_name = mohit,last_name = mathur,gender = male}"; 

value = value.substring(1, value.length()-1); //remove curly brackets

String[] keyValuePairs = value.split(","); //split the string to creat key-value pairs

Map<String,String> map = new HashMap<>();

for(String pair : keyValuePairs) //iterate over the pairs

{

String[] entry = pair.split("="); //split the pairs to get key and value

map.put(entry[0].trim(), entry[1].trim()); //add them to the hashmap and trim whitespaces

}

例如,您可以切换

value = value.substring(1, value.length()-1); 

value = StringUtils.substringBetween(value, "{", "}"); 

和像

在LabelModel 是

你应该投值后

回答:

SharedPreference不支持商店地图对象。 https://developer.android.com/reference/android/content/SharedPreferences.Editor.html。所以你的代码只是存储数据obj.toString,这就是为什么你看到调试结果。如果你想存储地图,你可以存储为json并使用put字符串。

回答:

相反转换地图GSON字符串和)共享偏好将其存储在偏好这样

//convert to string using gson 

Gson gson = new Gson();

String mapString = gson.toJson(map);

//save this in the shared prefs

SharedPreferences prefs = getSharedPreferences("test", MODE_PRIVATE);

prefs.edit().putString("yourkey", mapString).apply();

//get from shared prefs in gson and convert to maps again

String storedHashMapString = prefs.getString("yourkey",null);

java.lang.reflect.Type type = new TypeToken<HashMap<String, String>>(){}.getType();

//Get the hashMap

HashMap<String, String> map = gson.fromJson(storedHashMapString, type);

以上是 检索hashmap数据SharedPreferences 的全部内容, 来源链接: utcz.com/qa/261695.html

回到顶部