英文:
How to put Gson value into HashMap
问题
以下是已翻译的部分:
我有一个Firebase数据库,我想在我的应用程序中存储用户信息。在我的Firebase数据库中,我有4个字符串和1个类似于以下内容的数组列表:
(图片链接)
我想使用SharedPreferences将这些值存储在我的应用程序中。我已经检索了手机号码、出生日期、名字和性别,并将它们存储在一个HashMap中。但是我无法获取用户的爱好信息。我使用了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
函数中将用户的兴趣爱好与其他字符串一起放置?我希望能够加载和读取HashMap
中的所有数据。感谢您的理解,我的英文不太好。
英文:
I have a Firebase database and I want to store users information in my app. In my firebase database, I have 4 string and 1 arraylist like this..
I want to store these values in my app with using SharedPreferences. I did retrieve Phone number, Birthdate, First name and Gender and I stored into a Hashmap. But I couldn't get User hobbies. I use Gson library for User hobbies, but I don't know how to use with other string values.
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;
}
In createLoginSession, I defined a gson variable and I used putString value to KEY_HOBBIES like other strings. What should I do to put User Hobbies with other strings in Hashmap function? I want to load and read all datas in Hashmap. My english is not very good, thank you for all.
答案1
得分: 2
你可以像这样使用 jsonArray:
JSONArray jsonArray = object.getJSONArray("response");
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
// 在这里使用你的对象
}
英文:
You can use jsonArray like this
JSONArray jsonArray = object.getJSONArray("response");
for (int i = 0; i <= jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
//here you use your objects
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论