如何调用AsyncTask的调用Web服务之后更新列表视图

如何调用AsyncTask的调用Web服务之后更新列表视图

本文介绍了如何调用AsyncTask的调用Web服务之后更新列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话一个 Web服务的AsyncTask ,调用 Web服务我打电话一只叫方法 makeRequest() doInBackground(),我得到的回应另一种方法成功(),在成功的方法,我更新列表视图

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

但我得到的错误,如

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

下面林加入我的code.Im调用 synctask 从活动

Here Im adding my code.Im calling synctask from activity

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

这里是AsyncTask的类

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(Void... unused) {
             c.getTickets(url,sp);
            return null;
            //progress.setMessage("Loading...");
          }

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

下面即时得到 Web服务响应

 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();
    }
});

这篇关于如何调用AsyncTask的调用Web服务之后更新列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:22