Android Loading ListView from another Thread -


please explain me why code not work?(i need call lv.setadapter (adaptera) in onclick(). necessary change adapters). works if run code on main thread, need load data in thread. grateful ideas. thank you.

package com.example.examplelist;  import java.text.dateformat; import java.util.arraylist; import java.util.date; import java.util.concurrent.timeunit;  import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.listview;  public class activityelemsexample extends activity implements onclicklistener {     arraylist<elem> mylist;     button btn;     listview lv;     adaptera adaptera;     adapterb adapterb;     boolean running = false;     thread thread;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         btn = (button)findviewbyid(r.id.button_loading);         btn.setonclicklistener(this);         lv = (listview)findviewbyid(r.id.lv);          mylist = new arraylist<elem>();         adaptera = new adaptera(this, mylist);         adapterb = new adapterb(this, mylist);     }      public void onclick(view button){         if(!running){             running = true;             log.d("mylogs", "loading");              /*             if(...){                 lv.setadapter(adaptera);             }             else{                 lv.setadapter(adapterb);             }             */              lv.setadapter(adaptera);              thread = new thread(new runnable(){             public void run(){                 mylist.clear();                  for(int i=0; < 3000; i++){                     mylist.add(new elem(i, system.currenttimemillis(), "elem "+i));                 }                 handler.sendemptymessage(1);             }             });             thread.start();         }     }      public void ondestroy(){         super.ondestroy();         if(thread != null){             while(true){                 try{                     thread.join();                     break;                 }                 catch (interruptedexception e){}             }         }     }      public handler handler = new handler() {         public void handlemessage(android.os.message msg) {             /*              if(...){               }              else{               }              */              adaptera.notifydatasetinvalidated();             adaptera.notifydatasetchanged();              running = false;         }     }; } 

log:

fatal exception: main process: com.example.examplelist, pid:553 java.lang.illegalstateexception: content of adapter has changed listview did not receive notification. make sure content of adapter not modified background thread, ui thread. make sure adapter calls notifydatasetchanged() when contentges ... 

the problem is, modifying data of adapter while showing it. should make copy of data before adding more items, replace data in adapter , notify.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -