Dav*_*ria 3 android asynctaskloader android-loadermanager android-loader
我有一个AsyncTaskLoader<List<String>>成员变量List<String>mAddresses.在loadInBackground,如果我创建一个新对象,填充它,并返回它; onLoadFinished按预期调用,所以我可以更新适配器,然后更新视图.
public List<String> loadInBackground()
{
// ListView updates properly only if I do this (strange?)
mAddresses = new ArrayList<String>();
// ... fill mAddresses ...
return mAddresses;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用相同的变量,即使它的值发生变化也会onLoadFinished被调用:
public List<String> loadInBackground()
{
// ListView does not update with this!
if(mAddresses == null)
{
mAddresses = new ArrayList<String>();
}
// ... fill mAddresses ...
return mAddresses;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以评论这个吗?我是否应该像这样创建一个新对象来返回?是否有任何标志我可以设置"对象已更改,所以即使它是同一个对象,请确保执行所有更新?
如果数据已经加载(就像在加载程序中的'mAddresses'变量中那样),则需要调用forceLoad()以使其再次加载:
http://developer.android.com/reference/android/content/Loader.html#forceLoad%28%29
编辑: ...经过一些调查...回答你原来的问题,无益:'因为它没有'!
从LoaderManager的源代码:
// Notify of the new data so the app can switch out the old data before
// we try to destroy it.
if (mData != data || !mHaveData) {
mData = data;
mHaveData = true;
if (mStarted) {
callOnLoadFinished(loader, data);
}
}
Run Code Online (Sandbox Code Playgroud)
RE你的问题,我假设你不断在loadInBackground()中添加List而不是清除并重新启动.最干净的方法是每次通过将现有List作为参数传递给ArrayList()构造函数来构建新的ArrayList,它将复制内容.
或者您可以覆盖deliverResult(),它从主线程调用,因此修改mAdapter是正常的.
或者忘记Loader并使用更简单的AsyncTask ...虽然这取决于您的数据实际来自何处.
| 归档时间: |
|
| 查看次数: |
2504 次 |
| 最近记录: |