开发者

How to update listview after calling Asynctask to call webservice

I am calling a webservice through asynctask, to call the webservice i am calling one method named makeRequest() in doInBackground(), I am getting the response in another methods success(), In success method i am updating the listview

But i am getting error like

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."

Here Im adding my code.Im calling synctask from activity

new MyTask(this,urlAsString,sp).execute(); 

here is the Asynctask class

class MyTask extends AsyncTask<Void, Void, Void> {
        ProgressDialog progress;

        String url;
        SharedPreferences sp;
        HomepageH2desk c;
          public MyTask(HomepageH2desk context,String url,SharedPreferences sp) {
            this.c = context;
            this.url = url;
            this.sp = sp;
            progress = new ProgressDialog(this.c);
            progress.setMessage("Loading...");
          }

          public void onPreExecute() {
            progress.show();
          }

          public Void doInBackground(Vo开发者_StackOverflow中文版id... unused) {
             c.getTickets(url,sp);
            return null;
            //progress.setMessage("Loading...");
          }

          public void onPostExecute(Void unused) {
            progress.dismiss();
          }
        }

Here im getting webservice response

 public void success(Object result) {

     list = (ArrayList<Map<String, String>>) result;

    this.adapter.setList(list);
            this.adapter.notifyDataSetChanged();
}

listview is not getting updated and showing error

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Help me to solve this problem...


You should update your List like this.

Activity_name.this.runOnUiThread(new Runnable() {

@Override
public void run() {
      list = (ArrayList<Map<String, String>>) result;
      this.adapter.setList(list);
      this.adapter.notifyDataSetChanged();
    }
});


Since this the error that comes up when you do some MainThread task in another thread.....

try This:

runOnUiThread(new Runnable(){

public void run(){

     this.adapter.setList(list);
     this.adapter.notifyDataSetChanged();
   }

});

This code might have some errors But in simple Words.. add the notifyDataSetChanged call into runOnUiThread() method. You will be Done..

OR this can also be DOne ( the perfect way )

add the following in your activity class

 private Handler handler = new Handler() {
     @Override
     public void handleMessage(Message msg) {

        this.adapter.setList(list);
        adapter.setnotifyDataSetChanged();
     }
  };

Call this handler when and where you want to call the notifydatasetchanged like this

  handler.sendEmptyMessage(0);

Thanks sHaH


call the success method on onPostExecute method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜