Android ListView: how to avoid database query in bindView()? Need to fetch one to many relationship data

梦想的初衷 提交于 2019-12-11 02:56:45

问题


I have a list view to display albums. In each album list item, I need to display some information from each photo in this album. Here is how my cursor loader looks like:

@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();
}

Basically, I let the cursor loader manage the album query, and each time in the bindView I query the photos from the passed in album cursor. I feel this can be a performance issue. Is there a better way doing this?


回答1:


Just got an idea: use another cursor loader inside the list item. So this will be like two level cursor loader. I'll post my results later.

Edit:

The proposed idea should be the right way handling the issue. So my code is refactored like this:

// 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);
}

}


来源:https://stackoverflow.com/questions/21144964/android-listview-how-to-avoid-database-query-in-bindview-need-to-fetch-one-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!