英文:
Store Arraylist in SharedPreferences with Gson
问题
我刚开始学习Java和Android开发。我有一个数据库,其中包括性别、电话号码、姓名和爱好数组。我想使用SharedPreferences将这些信息存储在我的活动中,但我不知道如何应用Gson类。以下是我的代码:
public class SessionManager {
SharedPreferences usersSession;
SharedPreferences.Editor editor;
Context context;
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 ArrayList<String> KEY_HOBBIES = new ArrayList<>();
public SessionManager(Context _context) {
context = _context;
usersSession = _context.getSharedPreferences("userLoginSession", Context.MODE_PRIVATE);
editor = usersSession.edit();
Gson gson = new Gson();
String json = gson.toJson(KEY_HOBBIES);
}
public void createLoginSession(String name, String birthdate, String gender, String phone, ArrayList<String> hobbies) {
editor.putBoolean(IS_LOGIN, true);
Gson gson = new Gson();
String json = gson.toJson(hobbies);
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();
}
}
在这段代码中,我发现了2-3个错误。要如何将这个ArrayList与其他字符串值一起存储呢?谢谢
英文:
I am new in Java and Android development. I have a database that includes gender, phone number, name and hobby array. I want to store in my activity with SharedPreferences but I don't know how to apply Gson class. This is my code:
public class SessionManager {
SharedPreferences usersSession;
SharedPreferences.Editor editor;
Context context;
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 ArrayList<String> KEY_HOBBIES = new ArrayList<>();
public SessionManager(Context _context) {
context = _context;
usersSession = _context.getSharedPreferences("userLoginSession", Context.MODE_PRIVATE);
editor = usersSession.edit();
Gson gson = new Gson();
String json = gson.toJson(KEY_HOBBIES);
}
public void createLoginSession(String name, String birthdate, String gender, String phone, ArrayList<String> hobbies) {
editor.putBoolean(IS_LOGIN, true);
Gson gson = new Gson();
String json = gson.toJson(KEY_HOBBIES);
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();
}
I made 2-3 mistakes in this code, how can I store this Arraylist with other string values. Thank you
答案1
得分: 1
存储ArrayList
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hobbies.size(); i++) {
sb.append(hobbies.get(i)).append(",");
}
editor.putString(KEY_HOBBIES, sb.toString());
检索存储的数据
String[] array = playlist.split(",");
hobbies = new ArrayList<>(Arrays.asList(array));
英文:
to store ArrayList
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hobbies.size; i++) {
sb.append(hobbies.get(i)).append(",");
}
editor.putString(KEY_HOBBIES, sb.toString());
to retrieve it
String[] array = playlist.split(",");
hobbies = new ArrayList<>(Arrays.asList(array));
答案2
得分: 1
你可以使用 TYPE 来从 sharedPreferences 中读取数据
读取数据
Gson gson = new Gson();
String json = sharedPreferences.getString("Set", "");
Type type = new TypeToken<List<String>>() {
}.getType();
List<String> arrPackageData = gson.fromJson(json, type);
for(String data:arrPackageData) {
result.setText(data);
}
并且 保存数据
final ArrayList<String> arrPackage;
String nameS = name.getText().toString().trim();
String birhtdayS = birthday.getText().toString().trim();
String gendarS = gendar.getText().toString().trim();
String phoneS = phone.getText().toString().trim();
arrPackage.add(nameS);
arrPackage.add(birhtdayS);
arrPackage.add(gendarS);
arrPackage.add(phoneS);
Gson gson = new Gson();
String json = gson.toJson(arrPackage);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Set",json );
editor.commit();
}
英文:
You can use TYPE to read data from sharedPreferences
Read data
Gson gson = new Gson();
String json = sharedPreferences.getString("Set", "");
Type type = new TypeToken<List<String>>() {
}.getType();
List<String> arrPackageData = gson.fromJson(json, type);
for(String data:arrPackageData) {
result.setText(data);
}
and save data
final ArrayList<String> arrPackage;
String nameS = name.getText().toString().trim();
String birhtdayS = birthday.getText().toString().trim();
String gendarS = gendar.getText().toString().trim();
String phoneS = phone.getText().toString().trim();
arrPackage.add(nameS);
arrPackage.add(birhtdayS);
arrPackage.add(gendarS);
arrPackage.add(phoneS);
Gson gson = new Gson();
String json = gson.toJson(arrPackage);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Set",json );
editor.commit();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论