我有一个列表视图来显示专辑。在每个相册列表项中,我需要显示该相册中每张照片的一些信息。这是我的光标加载器的样子:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, AlbumsColumns.CONTENT_URI, null, null, null, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
long albumId = cursor.getLong(cursor.getColumnIndex(AlbumsColumns._ID));
Cursor photoCursor = getContentResolver().query(PhotosColumns.CONTENT_URI, null, PhotosColumns.ALBUM_ID + " = ?",
new String[]{Long.toString(albumId)}, null);
if (photoCursor.moveToFirst()){
do{
// Here I collect the information from each photo in the album
}while(photoCursor.moveToNext());
}
photoCursor.close();
}
基本上,我让光标加载器管理相册查询,每次在bindView中我都会从传入的相册光标中查询照片。我觉得这可能是一个性能问题。有更好的方法吗?
刚刚有了一个想法:在列表项中使用另一个游标加载器。所以这就像两级游标加载器。我稍后会发布我的结果。
Edit:
提出的想法应该是处理问题的正确方法。所以我的代码重构如下:
// In the list view activity:
@Override
public void bindView(View view, Context context, Cursor cursor) {
AlbumListItem albumListItem = (AlbumListItem)view;
albumListItem.prepareView(getLoaderManager(), cursor);
}
// AlbumListItem is a class that has a cursor loader to query photos:
public class AlbumListItem extends RelativeLayout implements LoaderManager.LoaderCallbacks<Cursor>{
public void prepareView(LoaderManager loaderManager, Cursor albumCursor){
albumId = albumCursor.getLong(albumCursor.getColumnIndex(AlbumsColumns._ID));
// Other init stuff
loaderManager.initLoader(UNIQUE_LOADER_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Here is the async query part.
return new CursorLoader(context, PhotosColumns.CONTENT_URI, null, PhotosColumns.ALBUM_ID + " = ?", new String[]{Long.toString(albumId)}, null);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)