Search code examples
androidandroid-cursoradapterandroid-listfragment

Getting custom adapter to skip header from addHeaderView()


I've got a ListFragment, which uses a cursorLoader and custom adapter to populate the views in a list. The list contains the albums by a particular band, and ideally the top of the list (header) would display the band name and "all songs" or something similar.

The problem is that when I use addHeaderView() to add the header to the list, the adapter treats the header as just another item in the list, and tries to fill it with the information that would ideally only fill the rows below the header.

I've tried using the following inside the bindView in the adapter:

if (cursor.getPosition() == 0)) {
return; }
else {bind the rest of the views}

But it seems to act strangely - sometimes not binding either of the first two rows..

Below is some code snippets:

Inside the listFragment:

    albumsAdapter = new AlbumAdapter(getActivity(), null, 0);

    setListAdapter(null);

    if (showHeader) {
        ViewGroup parent = (ViewGroup) getActivity().findViewById(
                R.id.relativeLayoutTest);
        View view = (View) getActivity().getLayoutInflater().inflate(
                R.layout.albumitem, parent, true);
        TextView textView = (TextView) getActivity().findViewById(
                R.id.artistTitle);
        textView.setText("Artist");

        getListView().addHeaderView(view);

    }

    setListAdapter(albumsAdapter);

And the custom adapter:

package another.music.player;

/*  imports etc
*/

class AlbumAdapter extends CursorAdapter {

public AlbumAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
}

private static Uri currentSongUri;

@Override
public void bindView(View view, Context context, Cursor cursor) {

    ImageView albumArt = (ImageView) view.getTag(R.id.albumArt);
    TextView text1 = (TextView) view.getTag(R.id.artistTitle);
    TextView text2 = (TextView) view.getTag(R.id.albumTitle);

    albumArt.setImageBitmap(null);

    text1.setText(cursor.getString(cursor
            .getColumnIndex(AudioColumns.ARTIST)));
    text2.setText(cursor.getString(cursor
            .getColumnIndex(AudioColumns.ALBUM)));

    String currentAlbumId = cursor.getString(cursor
            .getColumnIndex(BaseColumns._ID));

    Integer currentAlbumIdLong = Integer.parseInt(currentAlbumId);
    Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
    currentSongUri = ContentUris.withAppendedId(artworkUri,
            currentAlbumIdLong);

    ImageLoader imageLoader = new ImageLoader();
    imageLoader.load(currentSongUri, albumArt, context);

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.albumitem, null);
    view.setTag(R.id.albumArt, view.findViewById(R.id.albumArt));
    view.setTag(R.id.artistTitle, view.findViewById(R.id.artistTitle));
    view.setTag(R.id.albumTitle, view.findViewById(R.id.albumTitle));

    return view;
}

}

Solution

  • As I see it, your headerView uses the same layout file as the adapter. Change it's name and the View ids inside. This will solve your problem.