android - Modify View from a different thread -


am geting json server content of listview. fine, want create progressdialog while json getin called. problem need call json (with http method) in different thread , show results in listview user.

here doing, getin error "05-09 12:13:28.358: w/system.err(344): android.view.viewroot$calledfromwrongthreadexception: original thread created view hierarchy can touch views."

try {                    final progressdialog progdailog;             progdailog =progressdialog.show(this,"hi", "loading");              new thread(new runnable() {                  @override                 public void run() {                     try                      {                         jsonarray json = new jsonarray(executehttpget("http://m-devsnbp.insightlabs.cl/cuentos/json"));                         progdailog.cancel();                         arraylist<hashmap<string, string>> taleslist = new arraylist<hashmap<string, string>>();                         taleslist = jsontohash(json);                         adapter = new lazyadapter((activity) ctx, taleslist, 2);                             list.setadapter(adapter);                     }                      catch (interruptedexception e)                      {                         e.printstacktrace();                     } catch (jsonexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                    }             }).start();          } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         } 

how can update views different thread? or better way this?

you need update ui on main ui thread. use runonuithread

        runonuithread(new runnable() //run on ui thread                  {                   public void run()                    {                      progdailog.cancel();                      adapter = new lazyadapter((activity) ctx, taleslist, 2);                         list.setadapter(adapter);                      }                  }); 

also consider using asynctask. suggest ypu use asynctask

http://developer.android.com/reference/android/os/asynctask.html

        progressdialog pd;          lazyadapter adapter;           listview list;          arraylist<hashmap<string, string>> taleslist = new arraylist<hashmap<string, string>>();   

in activity oncreate()

        pd = new progressdialog(activityname.this);         pd.settitle("processing...");          new thetask().execute();  

define asynctask inner class in activity class

    class extends asynctask<void,void,void>     {     @override protected void onpreexecute() {    // todo auto-generated method stub       super.onpreexecute();       pd.show();  }  @override protected void doinbackground(void... params) {     // todo auto-generated method stub                  try                  {                     jsonarray json = new jsonarray(executehttpget("http://m-devsnbp.insightlabs.cl/cuentos/json"));                     taleslist = jsontohash(json);                  }                  catch (interruptedexception e)                  {                     e.printstacktrace();                 } catch (jsonexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 } catch (exception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }        return null;  }      @override protected void onpostexecute(void result) {     // todo auto-generated method stub     super.onpostexecute(result);             pd.dismiss();             adapter = new lazyadapter(acctivityname.this, taleslist, 2);                 list.setadapter(adapter);  }       } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -