Skip to content Skip to sidebar Skip to footer

Android ListView: How To Avoid Database Query In BindView()? Need To Fetch One To Many Relationship Data

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

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

}

Post a Comment for "Android ListView: How To Avoid Database Query In BindView()? Need To Fetch One To Many Relationship Data"