android - ArrayList Length gets 0 in Singleton -


i using singleton fetching data web service , storing resulting data object in arraylist. looks this:

public class datahelper {  private static datahelper instance = null;  private list<customclass> data = null;  protected datahelper() {     data = new arraylist<>(); }  public synchronized static datahelper getinstance() {     if(instance == null) {         instance = new datahelper();     }     return instance; }  public void fetchdata(){      backendlessdataquery query = new backendlessdataquery();     queryoptions options = new queryoptions();     options.setsortby(arrays.aslist("street"));     query.setqueryoptions(options);      customclass.findasync(query, new asynccallback<backendlesscollection<customclass>>() {         @override         public void handleresponse(backendlesscollection<customclass> response) {              int size = response.getcurrentpage().size();              if (size > 0) {                 adddata(response.getdata());                 response.nextpage(this);             } else {                 eventbus.getdefault().post(new fetcheddataevent(data));             }          }          @override         public void handlefault(backendlessfault fault) {             eventbus.getdefault().post(new backendlessfaultevent(fault));         }     });  }  public list<customclass> getdata(){     return this.data; }  public void setdata(list<customclass> data){     this.data = data; }  public void adddata(list<poster> data){     this.data.addall(data); }  public list<customclass> getdata(filterenum filter){      if(filter == filterenum.nofilter){         return getdata();     }else{         // filtering , returning filtered data     }      return getdata(); } } 

the data fetched correctly , list contains data after it. also, 1 instance created, intended. however, whenever call getdata later, length of this.data 0. because of tried subclass of application holding datahelper object, resulting in same problem.

  1. is there way of debugging this? there global watches in android studio?
  2. is there wrong approach? there better approach? ios developer, android pretty new me. showing data arraylist in different views, want have present in arraylist long application runs.

thanks!

edit: example use in list view fragment (only relevant parts):

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     filter = filterenum.nofilter;     data = datahelper.getinstance().getdata(filter); } @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) { customclasslistadapter = new customclasslistadapter(getactivity(), data);} 

edit2: added code fetch data backendless, changed reference of datahelper reference of data in first edit

edit3: usa local eventbus notifying list view new data. looks , works (initially data gets populated, after e.g. applying filter, arraylist getdata empty):

@subscribe public void onmessageevent(fetcheddataevent event) {     customclasslistadapter.notifydatasetchanged(); } 

try instead of keeping reference datahelper instance, keeping reference list of retrieved items. f.e. when first fetch list (and it's ok say), assign class member. or itarate through , create own array list of objects future use.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -