group similar rows in listview based on their content
Asked Answered
T

3

21

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

Tuttifrutti answered 22/12, 2010 at 8:3 Comment(3)
What kind of adapter are you currently using? Are you interested in how to display "separator" list items?Serg
+1 for the easy to understand paintingsRamirez
I want to perform this same task, but all the values will be updated run-time. please suggest me how to do this, I tried alot but getting repeated section list. In all not getting success.Syndic
L
1

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

Laboured answered 17/9, 2015 at 14:4 Comment(0)
G
0

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

Glasswork answered 25/5, 2014 at 12:23 Comment(0)
L
-1

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

}
Lichfield answered 8/1, 2015 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.