Groupie: How to update a particular item inside a section with headers & items?

Created on 22 Jan 2018  路  7Comments  路  Source: lisawray/groupie

I have multiple sections with headers and items inside.
Say->
Kitchen Header -> 1 to 10 items
Dining Room Header -> 1 to 5 items

I have a new item with updated fields but same id as the 6th item inside Kitchen Header, now by using this id i want to find out that 6th item inside my UI and replace that with my new item.

Inside the OnItemClickListener we get the reference of that particular item that has been clicked, like wise can I get the reference of the item using its id?

question

All 7 comments

Hmm, there isn't any mechanism for this built in.

Groupie was designed to operate by object reference, so you should at least hold onto a reference for the Section. Since you know how your section is structured, you may want to make a special RoomOfHouseSection, where you keep a reference to the list of children, which you could iterate and search by id -- or maybe a map of . All up to you.

You could also implement this using Section.getItemCount() and calling Section.getItem(position) repeatedly (e.g. from 0 to getItemCount - 1), but Groupie isn't optimized for this case of reverse lookup, so it will be slightly less efficient.

When you do get a reference to the Item, I recommend calling Item.notifyChanged(payload) to do a partial update.

Instead of passing the payload inside notifychanged, I'm using it like this:

private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(Item item, View view) {
            if (item instanceof ExtCamMediaItem) {
                ExtCamMediaItem cardItem = (ExtCamMediaItem) item;
                cardItem.setmThumbnailPath("some value changed");
                cardItem.notifyChanged();
            }
        }
    };

This is working fine for my case. What's the actual purpose of passing the payload and what should be the payload in my case?

Also I am passing unique id's to each media item when creating it.
So when I do

currentSection.add(new ExtCamMediaItem("1", 1));
currentSection.add(new ExtCamMediaItem("2", 2));

My result is like this which is correct.

section_add

But if i pass the collection of all the items like ArrayList to section.update like

ArrayList<ExtCamMediaItem> itemList;
itemList.add(new ExtCamMediaItem("1", 1));
itemList.add(new ExtCamMediaItem("2", 2));
currentSection.update(itemList);

I get this result

section_update

Why is this happening?

If i want to remove a single item on click of it inside click listener

private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(Item item, View view) {
            if (item instanceof ExtCamMediaItem) {
                ExtCamMediaItem cardItem = (ExtCamMediaItem) item; 
                // how to remove this item from the section?
            }
        }
    };

Ok wow, lots of questions here.

  • Why use a payload?
    The purpose of a payload is to do a partial bind. If binding an item is expensive or causes a visual artifact (like reloading a background image) you don't want to do that if just some text has changed or the user clicked "like". In this case, it doesn't sound like it matters whether you do a partial bind, or a full bind (the result of notifyChanged()), so do whatever you like! :)

  • Why does the header blink in your Section.update()?
    Not sure, because what you show in the gif isn't what I would expect from your code. It looks like the number in the header ("Kitchen 1", "Kitchen 2") is changing every time. I don't see that in your update code, and updating a section shouldn't touch the header.

  • How to remove an item from a section?

Section.remove(item)

Hi Lisa, thanks for a great library.

I am having an issue removing objects and I can't figure out why.

I am adding items to the adapter with a ChildEventListener to my Firebase Database.

Under on child added I have this following command:

bucketsAdapter.add(SingleBucketSuggestion(p0.key!!))

And under on child remove I have this one

bucketsAdapter.remove(SingleBucketSuggestion(p0.key!!))

But the app keeps crashing with this error:

2019-03-27 17:10:49.386 17380-17380/co.getdere E/AndroidRuntime: FATAL EXCEPTION: main
Process: co.getdere, PID: 17380
java.lang.IllegalArgumentException: fromIndex(0) > toIndex(-1)

Not sure what am I doing wrong as I am looking for the item exactly how I add it.

@Tsabary I'm getting the same exact exception on my adapter and I can't figure it out when I'm trying to remove a Section. Have you found any more information on the error?

@Tsabary @L4grange can you please raise a separate issue with a reproducible example and we can have a look. I'm closing this issue now since there's been no updates from the user that reported it.

@Tsabary I faced the same exception today and it turns out this exception occurs when you are trying to remove a group that is not (added) in the adapter, thus the -1 index.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattinger picture mattinger  路  3Comments

rpedretti picture rpedretti  路  3Comments

MirzaAhmedBaig picture MirzaAhmedBaig  路  6Comments

alvin-nt picture alvin-nt  路  3Comments

punitda picture punitda  路  3Comments