我有一个Firebase数据库,我想在我的应用程序中存储用户信息。在我的防火墙数据库中,我有4个字符串和1个数组,如下所示..

我想使用SharedPreferences将这些值存储在我的应用程序中。我确实检索了电话号码、生日、名字和性别,并将其存储在哈希地图中。但我不能得到用户的爱好。我使用Gson库作为用户爱好,但我不知道如何与其他字符串值一起使用。
public static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_NAME = "name";
public static final String KEY_BIRTHDATE = "birthdate";
public static final String KEY_GENDER = "gender";
public static final String KEY_PHONE = "phone";
public static final String KEY_HOBBIES = "hobbies";
public SessionManager(Context _context) {
context = _context;
usersSession = _context.getSharedPreferences("userLoginSession", Context.MODE_PRIVATE);
editor = usersSession.edit();
}
public void createLoginSession(String name, String birthdate, String gender, String phone, ArrayList<String> hobby) {
Gson gson = new Gson();
String json = gson.toJson(hobby);
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_NAME, name);
editor.putString(KEY_BIRTHDATE, birthdate);
editor.putString(KEY_GENDER, gender);
editor.putString(KEY_PHONE, phone);
editor.putString(KEY_HOBBIES, json);
editor.commit();
}
public HashMap<String, String> getUsersDetailFromSession() {
HashMap<String, String> userData = new HashMap<String, String>();
userData.put(KEY_NAME, usersSession.getString(KEY_NAME, null));
userData.put(KEY_BIRTHDATE, usersSession.getString(KEY_BIRTHDATE, null));
userData.put(KEY_GENDER, usersSession.getString(KEY_GENDER, null));
userData.put(KEY_PHONE, usersSession.getString(KEY_PHONE, null));
return userData;
}在createLoginSession中,我定义了一个gson变量,并像其他字符串一样使用putString值来KEY_HOBBIES。我应该怎么做才能把用户爱好和其他字符串放在Hashmap函数中呢?我想用哈希地图加载和阅读所有的数据。我的英语不是很好,谢谢大家。
发布于 2020-08-31 08:02:16
您可以像这样使用jsonArray
JSONArray jsonArray = object.getJSONArray("response");
for (int i = 0; i <= jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
//here you use your objects
}https://stackoverflow.com/questions/63666787
复制相似问题