将ArrayList使用Gson存储在SharedPreferences中。

huangapple go评论72阅读模式
英文:

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 = &quot;IsLoggedIn&quot;;
public static final String KEY_NAME = &quot;name&quot;;
public static final String KEY_BIRTHDATE = &quot;birthdate&quot;;
public static final String KEY_GENDER = &quot;gender&quot;;
public static final String KEY_PHONE = &quot;phone&quot;;
public static final ArrayList&lt;String&gt; KEY_HOBBIES = new ArrayList&lt;&gt;();
public SessionManager(Context _context) {
context = _context;
usersSession = _context.getSharedPreferences(&quot;userLoginSession&quot;, 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&lt;String&gt; 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 &lt; hobbies.size; i++) {
sb.append(hobbies.get(i)).append(&quot;,&quot;);
}
editor.putString(KEY_HOBBIES, sb.toString());

to retrieve it

    String[] array = playlist.split(&quot;,&quot;);
hobbies = new ArrayList&lt;&gt;(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(&quot;Set&quot;, &quot;&quot;);
Type type = new TypeToken&lt;List&lt;String&gt;&gt;() {
}.getType();
List&lt;String&gt; arrPackageData = gson.fromJson(json, type);
for(String data:arrPackageData) {
result.setText(data);
}

and save data

        final ArrayList&lt;String&gt; 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(&quot;Set&quot;,json );
editor.commit();
}

huangapple
  • 本文由 发表于 2020年8月31日 04:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63661869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定