具有多个加载器的 LoaderManager:如何获得正确的光标加载器
对我来说,如果您有多个加载器,则不清楚如何获得正确的光标。假设您定义了两个不同的 Loader:
getLoaderManager().initLoader(0,null,this);
getLoaderManager().initLoader(1,null,this);
然后在 onCreateLoader() 中您根据 id 执行不同的操作:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
if (id==0){
CursorLoader loader = new CursorLoader(getActivity(),
MaterialContentProvider.CONTENT_URI,null,null,null,null);
}else{
CursorLoader loader = new CursorLoader(getActivity(),
CustomerContentProvider.CONTENT_URI,null,null,null,null);
};
return loader;
}
到目前为止一切顺利。但是如何在 onLoadFinished() 中获取正确的光标,因为您没有获得任何 id 来识别正确 Cursoradapter 的正确 Cursor。
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
mycursoradapter1.swapCursor(cursor);
if(isResumed()){
setListShown(true);
}else {
setListShownNoAnimation(true);
}
}
//and where to get the cursor for mycursoradapter2
或者我错了,这是在一个片段中获取两个不同光标适配器结果的错误方法。
To me it's not clear how to get the right cursor if you have multiple Loaders. Lets say you define two different Loader with:
getLoaderManager().initLoader(0,null,this);
getLoaderManager().initLoader(1,null,this);
then in onCreateLoader() you do different things depending on the id:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
if (id==0){
CursorLoader loader = new CursorLoader(getActivity(),
MaterialContentProvider.CONTENT_URI,null,null,null,null);
}else{
CursorLoader loader = new CursorLoader(getActivity(),
CustomerContentProvider.CONTENT_URI,null,null,null,null);
};
return loader;
}
so far so good. But how to get the right cursor in onLoadFinished() because you don't get any id to identify the right Cursor for the right Cursoradapter.
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
mycursoradapter1.swapCursor(cursor);
if(isResumed()){
setListShown(true);
}else {
setListShownNoAnimation(true);
}
}
//and where to get the cursor for mycursoradapter2
or am I wrong and this is the wrong way to get results for two different cursoradapter in one fragment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Loader 类有一个名为 getId()。我希望这会返回您与加载程序关联的 id。
The Loader class has a method called getId(). I would hope this returns the id you've associated with the loader.
使用Loader的getId()方法:
Use the getId() method of Loader:
如果您的加载器除了结果的类类型(此处:Cursor)之外没有任何共同点,那么您最好创建两个单独的 LoaderCallbacks 实例(简单地作为两个内部类)在你的活动/片段中),每个都专门用于一种加载程序处理,而不是尝试将苹果与橙子混合。
在您的情况下,数据源和结果处理似乎都不同,这需要您编写额外的样板代码来识别当前场景并将其分派到适当的代码块。
If your loaders have nothing in common but the class type of the result (here:
Cursor
), you're better off creating two separateLoaderCallbacks
instances (simply as two inner classes in your Activity/Fragment), each one dedicated to one loader treatment, rather than trying to mix apples with oranges.In your case it seems that both the data source and the result treatment are different, which requires you to write the extra boilerplate code to identify the current scenario and dispatch it to the appropriate code block.