调用Asynctask调用webservice后如何更新listview
我正在通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该像这样更新您的列表。
You should update your List like this.
由于这是当您在另一个线程中执行某些 MainThread 任务时出现的错误.....
尝试这个:
此代码可能有一些错误但简单地说..将notifyDataSetChanged 调用添加到 runOnUiThread() 方法中。您将完成..
或者这也可以完成(完美的方式)
在您的活动类中添加以下内容
在您想要像这样调用notifydatasetchanged的时间和地点调用此处理程序
谢谢
沙哈
Since this the error that comes up when you do some MainThread task in another thread.....
try This:
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
Call this handler when and where you want to call the notifydatasetchanged like this
Thanks
sHaH
在 onPostExecute 方法上调用 success 方法
call the success method on onPostExecute method