pavelkorolevxyz
pavelkorolevxyz

Reputation: 1500

StickyGridHeaders custom adapter

I'm trying to implement my own StickyGridHeadersBaseAdapter, my current source code here - http://paste.org.ru/?11jrjh, and I use it like

ModeAdapter adapter = new ModeAdapter(this);
modeGridView.setAdapter(adapter);

Problems which I have is that

1) I have no idea how to call notifyDataSetChanged() for this adapter, so I can't change items

2) And implementation of AdapterView.OnItemClickListener (http://paste.org.ru/?mvgt7b) works strange

Mode mode = (Mode) adapter.getItem(position);

returns null for items with 1st and 2nd positions, item on 3rd position is actual 1st item in adapter.

Where is my fault here?

One more question is why I can't cast adapterView.getAdapter() in my OnItemClickListener to my ModeAdapter class.

What if I want to call notifyDataSetChanged() here? I didn't find any examples for custom implementation of StickyGridHeadersBaseAdapter here.

Thanks in advance.

Upvotes: 0

Views: 974

Answers (2)

a13xg0
a13xg0

Reputation: 407

An easier way to get an item would be:

StickyGridHeadersBaseAdapterWrapper wrapper = (StickyGridHeadersBaseAdapterWrapper) parent.getAdapter();
Mode item = (Mode ) wrapper.getWrappedAdapter().getItem(position);

Upvotes: 1

Stefaan Moreels
Stefaan Moreels

Reputation: 11

I had the same issue as you 2):

after the first header i got the item of the previous row, after the second header got the item of two rows up, etc...

The reason is the following:

StickyHeadersGridView:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    mOnItemClickListener.onItemClick(parent, view, mAdapter.translatePosition(position).mPosition, id);
}

The position is corrected. so in your onItemClick you get the corrected value of position.

If than you request the item with: Mode mode = (Mode) adapter.getItem(position); you get StickyGridHeadersBaseAdapterWrapper.getItem(int pos)

@Override
public Object getItem(int position) throws ArrayIndexOutOfBoundsException {
    Position adapterPosition = translatePosition(position);
    if (adapterPosition.mPosition == POSITION_FILLER || adapterPosition.mPosition == POSITION_HEADER) {
        // Fake entry in view.
        return null;
    }
    return mDelegate.getItem(adapterPosition.mPosition);
}

In StickyGridHeadersBaseAdapterWrapper.getItem() position gets corrected for the second time which causes the wrong item to be returned...

I added a work-around:

In StickyHeadersBaseAdapterWrapper I added:

public Object getItemAtDelegatePosition(int pos) {
    return mDelegate.getItem(pos);
}

And I use this in onItemClick:

Mode item = (Mode) ((StickyGridHeadersBaseAdapterWrapper)parent.getAdapter()).getItemAtDelegatePosition(position);

Upvotes: 1

Related Questions