FastAdapter icon indicating copy to clipboard operation
FastAdapter copied to clipboard

IndexOutOfBoundsException when inserting an item

Open victor5171 opened this issue 10 years ago • 12 comments

I'm using 3 adapters (FooterAdapter, FastItemAdapter and HeaderAdapter) and almost everything is working smoothly.

But when i have an item added in the HeaderAdapter and add an item in the FastItemAdapter the app crashes, reporting an IndexOutOfBoundsException, the stack trace is below:

java.lang.IndexOutOfBoundsException: Invalid index -1, size is 28
                      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                      at java.util.ArrayList.addAll(ArrayList.java:226)
                      at com.mikepenz.fastadapter.adapters.ItemAdapter.add(ItemAdapter.java:475)
                      at com.mikepenz.fastadapter.adapters.ItemAdapter.add(ItemAdapter.java:461)
                      at com.mikepenz.fastadapter.adapters.FastItemAdapter.add(FastItemAdapter.java:241)
                      at com.sexlog.sexlog.chat.ChatActivity.createAndAddMessage(ChatActivity.java:726)
                      at com.sexlog.sexlog.chat.ChatActivity.onMessageReceived(ChatActivity.java:699)
                      at com.sexlog.sexlog.chat.ChatActivity_.access$001(ChatActivity_.java:57)
                      at com.sexlog.sexlog.chat.ChatActivity_$7.run(ChatActivity_.java:306)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5343)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

As i can see, the problem is related to add an item, or a collection of items to a specific position in the composed adapter.

Can someone help me? Thanks!

Note: I'm using the following dependencies:

com.mikepenz:fastadapter:2.0.0@aar com.mikepenz:fastadapter-extensions:2.0.0@aar

victor5171 avatar Nov 30 '16 18:11 victor5171

how do you add those? in which order? on the main thread?

mikepenz avatar Nov 30 '16 18:11 mikepenz

I'm writing a chat app, so i add an item to the header adapter warning the user that the other user is typing a message (similar to the Facebook Messenger), and the crash occurs when the message is received.

The arrived message is added on the main thread, a few seconds after the header item was added.

victor5171 avatar Nov 30 '16 18:11 victor5171

but both are added on the main thread. and you use the methods provided by the adapter?

mikepenz avatar Nov 30 '16 19:11 mikepenz

Yes, for the header adapter: headerAdapter.add(writingItem);

And for the FastItemAdapter (the chat messages): chatItemAdapter.add(0, chatItem)

victor5171 avatar Nov 30 '16 19:11 victor5171

Hi @mikepenz ! any updates on this?

Thanks!

victor5171 avatar Dec 01 '16 15:12 victor5171

Mike, i've cloned the library and the following line is throwing the exception: ItemAdapter::add:382 mItems.addAll(position - getFastAdapter().getPreItemCountByOrder(getOrder()), items);

While i was debugging, and looking, this statement returns -1. But the mItems List just contains only the FastItemAdapter items, not the HeaderAdapter items, so isn't it correct to just remove the getFastAdapter().getPreItemCountByOrder and insert the item in the real position? What is it doing?

I've changed the line to mItems.addAll(position, items); and issue is solved.

victor5171 avatar Dec 02 '16 14:12 victor5171

The getFastAdapter().getPreItemCountByOrder(getOrder()) is required as the .add() method asks for the global position. So this is the same everywhere. To then make sure we have the adapter specific "local" position we remove the item count prior to this adapter.

It shouldn't return -1 though. can you send the sample you tried to reproduce this issue? I would love to fix the root issue of it returning -1

mikepenz avatar Dec 02 '16 21:12 mikepenz

It's easy. You only need to use a composed adapter with a header, a footer and a fastItemAdapter.

Then you insert an item in the header adapter and after that you should insert an item to the fastItemAdapter to the 0 position.

Like that: fastItemAdapter.add(0, Item)

The getPreItem calculation will return 1 because it counts the only existing header item and stops on the fastItemAdapter (order=500)

Position=0 - 1.

Hope it will help you.

victor5171 avatar Dec 02 '16 22:12 victor5171

@victor5171 oh now I see.

As of the concept of the fastAdapter it is correct that this does not work.

The methods (also mentioned in the javadoc) require you to pass the global position. So this means that if you have an header with 1 item you can only start to add items at position 1 in the second adapter. This decision was made to make sure we have a correct and clear api for all methods, and no jumping.

You can use the same helper methods as used internally (if you don't know the count of items in other adapters)

Or you use this method which directly gives you the correct global position: https://github.com/mikepenz/FastAdapter/blob/develop/library-core/src/main/java/com/mikepenz/fastadapter/adapters/ItemAdapter.java#L213

mikepenz avatar Dec 02 '16 22:12 mikepenz

Ok! Thanks!

You will try it.

I thought that the adapters use a local index, so 0 meant 1.

Everywhere i insert an item in a specific index i will change using this safest approach.

Anyway, thanks.

victor5171 avatar Dec 02 '16 22:12 victor5171

This decision was made to make sure we have a correct and clear api for all methods, and no jumping.

Do you mind explaining that in more details? I find this API very counterintuitive as it is.

renanferrari avatar Dec 03 '16 00:12 renanferrari

@renanferrari it was done so we can always work with the same position, and you do not have to mix between different positions as the fastadapter can only work with the global position. and we anyway have to notify within again with the global position.

If you feel uncomfortable with it. You can simply implement the IItemAdapter interface or extend the ItemAdapter and just change the specific methods to work with the relative position ;)

mikepenz avatar Dec 03 '16 08:12 mikepenz