从SharedPrefernce中检索ArrayList

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

Retrive ArrayList from SharedPrefernce

问题

以下是翻译好的内容:

我有一个用于在我的应用程序中保存和获取ArrayList的函数,如下所示:

public void saveArrayList(ArrayList<String> list, String key) {
    Gson gson = new Gson();
    String json = gson.toJson(list);
    editor.putString(key, json);
    editor.apply(); // 这一行很重要!!!
}

public ArrayList<String> getArrayList(String key) {
    Gson gson = new Gson();
    String json = pref.getString(key, "");
    Type type = new TypeToken<ArrayList<String>>() {}.getType();
    return gson.fromJson(json, type);
}

我试图在我的一个片段中检索它,如下所示:

ArrayList<String> UserVideoLang;

在创建时如下所示:

if (getActivity() != null) {
    customDialogBuilder = new CustomDialogBuilder(getActivity());
    parentViewModel = ViewModelProviders.of(getActivity()).get(HomeViewModel.class);
    sessionManager = new SessionManager(getActivity());
    UserVideoLang= sessionManager.getArrayList("UserVideoLang");
    if(UserVideoLang.isEmpty()){
        UserVideoLang.add("all");
    }
}

当我运行时,在下面这行代码上得到了NullPointerException:

if(UserVideoLang.isEmpty()){

完整的错误如下:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.isEmpty()' on a null object reference
    at com.vedagames.wowtok.view.home.ForUFragment.onActivityCreated

请告诉我我漏掉了什么,以及我应该如何修复它,谢谢!

英文:

I have function for save and get arraylist in my application like below

public void saveArrayList(ArrayList&lt;String&gt; list, String key){

        Gson gson = new Gson();
        String json = gson.toJson(list);
        editor.putString(key, json);
        editor.apply();     // This line is IMPORTANT !!!
    }

    public ArrayList&lt;String&gt; getArrayList(String key){
        Gson gson = new Gson();
        String json = pref.getString(key, &quot;&quot;);
        Type type = new TypeToken&lt;ArrayList&lt;String&gt;&gt;() {}.getType();
        return gson.fromJson(json, type);
    }

I am trying to retrive it in my one of fragment like below

ArrayList&lt;String&gt; UserVideoLang;

and on create its like below

if (getActivity() != null) {
            customDialogBuilder = new CustomDialogBuilder(getActivity());
            parentViewModel = ViewModelProviders.of(getActivity()).get(HomeViewModel.class);
            sessionManager = new SessionManager(getActivity());
            UserVideoLang= sessionManager.getArrayList(&quot;UserVideoLang&quot;);
            if(UserVideoLang.isEmpty()){
                UserVideoLang.add(&quot;all&quot;);
            }
        }

when I run I am getting NullPointerException on below line

if(UserVideoLang.isEmpty()){

and full error is like below

java.lang.NullPointerException: Attempt to invoke virtual method &#39;boolean java.util.ArrayList.isEmpty()&#39; on a null object reference
        at com.vedagames.wowtok.view.home.ForUFragment.onActivityCreated

Let me know what I am missing and how I should fix it, Thanks!

答案1

得分: 0

用以下代码替换你的 getArrayList 方法:

public ArrayList<String> getArrayList(String key){
    Gson gson = new Gson();
    String json = pref.getString(key, "[]");
    Type type = new TypeToken<ArrayList<String>>() {}.getType();
    return gson.fromJson(json, type);
}

传递默认值 [] 表示空的 JSON 数组。因此,如果在共享首选项中没有值,将返回一个空数组列表。

英文:

Replace your getArrayList method with this

public ArrayList&lt;String&gt; getArrayList(String key){
        Gson gson = new Gson();
        String json = pref.getString(key, &quot;[]&quot;);
        Type type = new TypeToken&lt;ArrayList&lt;String&gt;&gt;() {}.getType();
        return gson.fromJson(json, type);
    }

passing default value [] means empty JSON array. so if no value in shared preference empty array list will be returned.

答案2

得分: 0

Sharedpref 用于保存应用程序的小型数据。我不知道 ArrayList 的大小以及是单个还是多个 ArrayList。如果您的数据很大,应选择 Room 数据库来保存数据。您可以在 此链接 上查看有关 Room 的信息。

或者,对于空值检查,首先检查

UserVideoLang != null

然后检查

UserVideoLang.isEmpty()

如果在将 Gson 解析为类对象时出现问题,您可以使用更简单的技术,如...

UserVideoLangContainer container = new Gson().fromJson(userInfo, UserVideoLangContainer.class);

其中 UserVideoLangContainer.class 类似于

public class UserVideoLangContainer implements Serializable {
    public List<String> list;
}
英文:

Sharedpref is used to same small data for application. I don't know the size of the arraylist and single or multiple arraylist. If your data is large, you should choose Room Database for saving the data. You can check Room on this link.

OR,
For null check, first check

UserVideoLang != null

then check

UserVideoLang.isEmpty()

If there is problem with parsing Gson to Class object, you can use a easier technique for this. Like...

UserVideoLangContainer container = new Gson().fromJson(userInfo, UserVideoLangContainer.class);

Where UserVideoLangContainer.class is like

public class UserVideoLangContainer implements Serializable {
    public List&lt;String&gt; list;
}

huangapple
  • 本文由 发表于 2020年8月5日 18:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63262982.html
匿名

发表评论

匿名网友

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

确定