android-从自定义适配器类调用的AsyncTask更新ListView [英] android - Updating ListView from AsyncTask called from custom adapter class
本文介绍了android-从自定义适配器类调用的AsyncTask更新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从AsyncTask更新ListView.我有这种情况:
I am trying to update a ListView from an AsyncTask. I have this situation:
- 活动类从自定义Adapter类创建列表适配器.
- 适配器类在列表项中的元素上设置onClickListener,并调用位于不同实用程序类中的AsyncTask.
如何从实用程序AsyncTask中的onPostExecute()方法调用notifyDataSetChanged?
How can I call notifyDataSetChanged from onPostExecute() method in the Utilities AsyncTask?
推荐答案
包括一个 Delegate 作为回调,以从原始类中激活刷新.
Include a Delegate as a callback to activate the refresh from within the original class.
例如,添加以下界面:
public interface AsyncDelegate {
public void asyncComplete(boolean success);
}
然后,在您的调用类中,实现该接口,并将其传递给您的任务
Then, in your calling class, implement the interface, and pass it to your task
public class MyClass implements AsyncDelegate {
// Class stuff
MyAsyncTask newTask = new MyAsyncTask(this);
newTask.execute();
public void asyncComplete(boolean success){
myAdapter.notifyDataSetChanged();
}
在AsyncTask中,返回结果:
In the AsyncTask, return the result:
private class MyAsyncTask extends AsyncTask<Object, Object, Object> {
private AsyncDelegate delegate;
public MyAsyncTask (AsyncDelegate delegate){
this.delegate = delegate;
}
@Override
protected void onPostExecute(String result) {
delegate.asyncComplete(true);
}
}
任务完成后,它将调用委托,这将触发刷新!
When the task completes, it will call the delegate, which will trigger the refresh!
这篇关于android-从自定义适配器类调用的AsyncTask更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文