java - How to store and retrieve List<MyClass> object using RxPreferences -


i’m using gsonpreferenceadapter shown here prateek's rxpreferences library.

my fragment class code:

@inject rxsharedpreferences sharedpreferences; @inject gson gson;  list<myclass> my_class_list;  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {      // binding , inject go here.      class listtype = new typetoken<arraylist<myclass>>(){}.getclass();     gsonpreferenceadapter<list<myclass>> adapter = new gsonpreferenceadapter<>(gson, listtype, new arraylist<myclass>(), gsonpreferenceadapter.syntaxexceptionbehavior.null);     my_class_list = sharedpreferences.getobject("pref_myclass", new arraylist<>(), adapter).get();      if(my_class_list == null)         timber.i("tis null.");      timber.i("printing myclass items...");     for(myclass item: my_class_list)         timber.i("myclass: " + item);  }  @onclick(r.id.add) public void add() {      myclass entry = new myclass("count dracula", "12345");     my_class_list.add(entry);      class listtype = new typetoken<list<myclass>>(){}.getclass();     gsonpreferenceadapter<list<myclass>> adapter = new gsonpreferenceadapter<>(gson, listtype, new arraylist<myclass>(), gsonpreferenceadapter.syntaxexceptionbehavior.null);     sharedpreferences.getobject("pref_myclass", new arraylist<>(), adapter).set(my_class_list);      timber.i("data saved");     return false; } 

when run first time, doesn't log tis null means my_class_list not null. , click on button call add() , returns data saved suggesting indeed saved. when re-launch app crashes @ loop with:

attempt invoke interface method 'java.util.iterator java.util.list.iterator()' on null object reference 

it prints tis null i'm surprised how null added. what's going on here, , how solve this?

contents of myclass:

public class myclass {      @serializedname("name")     @expose     public string name;      @serializedname("number")     @expose     public string number;      public myclass(string name, string number) {         this.name = name;         this.number = number;     }      @override     public string tostring() {         return "myclass{" +                 "name='" + name + '\'' +                 ", number='" + number + '\'' +                 '}';     }  } 

never mind figured out on own. i’ve put answer if else comes across same issue. problem gsonpreferenceadapter ripped off. changed class type , worked fine.

type type = new typetoken<list<myclass>>(){}.gettype(); gsonpreferenceadapter<list< myclass >> adapter = new gsonpreferenceadapter<>(gson, type, new arraylist<>(), gsonpreferenceadapter.syntaxexceptionbehavior.null); my_class_list = sharedpreferences.getobject("pref_myclass", new arraylist<>(), adapter).get(); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -