android – group similar rows in listview based on their content-ThrowExceptions

Exception or error:

i have a listview that displays a set of rows, each row is clickable.
now, i wish to group similar type of rows under one header something like as shown in the figure (mocked up). could some please advise or provide an approach for this.
mockup image

How to solve:

Add an extra paremeter for the ‘category’ of each listview item. Then implement something like ‘StickyListHeaders’ based on that parameter.

###

I’ve used AmazingListView in the past with some effectiveness.
It is an implementation of the Google I/O Schedule App suggested approach.

Things that I like about it:

  1. Sticky headers
  2. Pagination with lazy loading

Of note:

  1. It’s an SVN-based project
  2. You need to include it as a library
  3. It’s harder to use it with Android Studio; much easier with ADT.

(I did just post a question about this recently)

Here’s an image from the project home page:
AmazingListView example lists

###

If your adapter is Cursor based then use SectionCursorAdapter 2.0, you can’t do simplier:

public class MyAdapter extends SectionCursorAdapter<String, MyAdapter.SectionViewHolder, MyAdapter.ItemViewHolder> {

    public MyAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0, R.layout.item_section, R.layout.item_title);
    }

    // this method will fullfill your wishes
    @Override protected String getSectionFromCursor(Cursor cursor) {
        return cursor.getString(cursor.getColumnIndexOrThrow("group"));
    }

// replace getView/bindView/newView
// --------------------------------------------
    @Override protected SectionViewHolder createSectionViewHolder(View sectionView, String section) {
        return new SectionViewHolder(sectionView);
    }

    @Override protected ItemViewHolder createItemViewHolder(Cursor cursor, View itemView) {
        return new ItemViewHolder(itemView);
    }

    @Override protected void bindSectionViewHolder(int position, SectionViewHolder sectionViewHolder, ViewGroup parent, String section) {
        sectionViewHolder.titleView.setText(section);
    }

    @Override protected void bindItemViewHolder(ItemViewHolder itemViewHolder, Cursor cursor, ViewGroup parent) {
        itemViewHolder.title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
        itemViewHolder.titleView.setText(itemViewHolder.text);
    }

// view holders
// --------------------------------------------
    public class SectionViewHolder extends ViewHolder {
        public TextView titleView;

        public SectionViewHolder(View rootView) {
            super(rootView);
            titleView = findWidgetById(R.id.sectionText);
        }
    }

    public class ItemViewHolder extends ViewHolder {
        public String title;
        public TextView titleView;

        public ItemViewHolder(View rootView) {
            super(rootView);
            titleView = findWidgetById(R.id.titleText);
        }
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *