调用Asynctask调用webservice后如何更新listview

发布于 2024-11-27 18:43:10 字数 1868 浏览 1 评论 0原文

我正在通过 asynctask 调用 webservice,为了调用 webservice 我正在调用一个名为 makeRequest() 的方法doInBackground(),我在另一个方法 success() 中得到响应,在 success 方法中我正在更新 listview

但我收到错误就像

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

在这里我添加我的代码。我正在打电话 这里的活动中的 synctask

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

是 Asynctask 类

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

,我在这里收到 webservice 响应

 public void success(Object result) {

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

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

listview 没有更新并显示错误

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

帮助我解决这个问题。 。

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(Void... 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2024-12-04 18:43:10

您应该像这样更新您的列表。

Activity_name.this.runOnUiThread(new Runnable() {

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

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();
    }
});
献世佛 2024-12-04 18:43:10

由于这是当您在另一个线程中执行某些 MainThread 任务时出现的错误.....

尝试这个:

runOnUiThread(new Runnable(){

public void run(){

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

});

此代码可能有一些错误但简单地说..将notifyDataSetChanged 调用添加到 runOnUiThread() 方法中。您将完成..

或者这也可以完成(完美的方式)

在您的活动类中添加以下内容

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

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

在您想要像这样调用notifydatasetchanged的时间和地点调用此处理程序

  handler.sendEmptyMessage(0);

谢谢
沙哈

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

不及他 2024-12-04 18:43:10

在 onPostExecute 方法上调用 success 方法

call the success method on onPostExecute method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文