Paul
Paul

Reputation: 498

Duplicates in my ExpandableListView Adapter

the problem goes is that everytime I add more than one Child to a GroupItem in my ExpandableListView, the item duplicates itself with the previous child. I have narrowed it down to the getChild() method in my ExpandableListView (custom) adapter.

The getChild() method looks like this:

public Object getChild(int groupPosition, int childPosition) {

     return rCollection.get(weekData.get(groupPosition)).get(childPosition);

}

Where rCollection = Map<String, List<Custom>>; and weekData = List<String>;

How do I change the getChild to return only one item per request so I dont get duplicates? I cannot use a for-loop because if I change the groupPosition and childPosition to Arrays I will have to implement the method again.

To Illustrate, this is what the issue looks like:

Duplicates

How do I exclude the Item 2 from being picked up by the first time it is called, and how do I exclude item 1 from being called the second time it is called? And so on.

Thank you for reading and all the help.

Upvotes: 1

Views: 1925

Answers (3)

Michael Wayne
Michael Wayne

Reputation: 23

am not sure if you found a solution but here is what worked for me; since i was returning a recyclerview in the child view, just return 1 in getChildrenCount() method.

That's it

Upvotes: 1

Paul
Paul

Reputation: 498

I dug it out of some old repo, I think this is what I ended up doing.

public int getChildrenCount(int groupPosition) {
    // Fixes a bunch of NPE when the groupPosition = NULL. This way it checks and returns 0 when it = null. 
    List<MySQLWeek> group = rCollection.get(weekData.get(groupPosition));

        if (group == null){
        return 0;
        }

        return group.size();


}

Upvotes: 0

johntheripp3r
johntheripp3r

Reputation: 989

@Override
public int getChildCount(.....)
{
     return 2;
}

If you are sure that for every item there are only 2 subitems
Sorry my bad this is the right method you want.
EDIT

@Override
public int getChildrenCount(int groupPosition)
{
    return (rCollection.get(weekdata.get(groupPosition())).size();
}

This will return the size (length) of your List<Custom> for a map.
balance paranthesis if needed

Upvotes: 2

Related Questions