FastAdapter icon indicating copy to clipboard operation
FastAdapter copied to clipboard

Class Cast Exception - Expandable Extension

Open sid-dob opened this issue 8 years ago • 4 comments

About this issue

  • Crash - Fatal Exception: java.lang.ClassCastException - Child Expandable cannot be cast to Parent Expandable on FastAdapter - onClickListener
  • Not Always , but if you open and close multiple times and scroll up and down and the expand, it crashes.

Details

  • [ ] fastadapter:3.3.1@aar
  • [ ] fastadapter-extensions-expandable:3.3.1
  • [ ] build:gradle:3.2.1
  • [ ] 3.2.1

Checklist

sid-dob avatar Nov 02 '18 07:11 sid-dob

@sid-dob please make sure your adapter type definition is a super type of all your items.

If you have the following

SuperType

  • ChildTypeA extends SuperType
  • ChildTypeB extends SuperType

Then the following applies

FastAdapter is only supposed to have ChildTypeA as content, that is also true for sub types (sadly Java's generics is not powerful enough to limit that)

FastAdapter is only supposed to have ChildTypeB as content

FastAdapter you can use both types inside this adapter

mikepenz avatar Nov 02 '18 08:11 mikepenz

Here , do you mean ->

ChildTypeA = ExpandableParent ChildTypeB = Expandablee ( i.e. the item that shows up after expansion )

Is this complex ? or am I missing on something ? Here are the definitions :

@Suppress("FINITE_BOUNDS_VIOLATION_IN_JAVA") class ProductDetailExpandableItem<Parent, SubItem>( val productDetailState: ProductDetailExpandableItemState ) : AbstractExpandableItem<ProductDetailExpandableItem<Parent, SubItem>, ProductDetailExpandableItemViewHolder, SubItem>() where Parent : IItem<*, *>, Parent : IExpandable<*, *>, SubItem : IItem<*, *>, SubItem : ISubItem<*, *>

Expandee @Suppress("FINITE_BOUNDS_VIOLATION_IN_JAVA") class ProductDetailExpandableDetailsItem<Parent>( val data: String?, val viewType: Int ) : AbstractExpandableItem<Parent, FastAdapter.ViewHolder<*>, ProductDetailExpandableDetailsItem<Parent>>() where Parent : IItem<*, *>, Parent : IExpandable<*, *>, Parent : IClickable<*>, Parent : ISubItem<*, *>

sid-dob avatar Nov 02 '18 09:11 sid-dob

What type definition does your adapter have?

FastAdapter<IItem> FastAdapter<ProductDetailExpandableItem> FastAdapter<AbstractExpandableItem>

The first super type of your items is the AbstractExpandableItem so you can do FastAdapter< AbstractExpandableItem> alternative IItem will work always

mikepenz avatar Nov 02 '18 10:11 mikepenz

I am using this :

val fastItemAdapter = FastItemAdapter<ProductDetailExpandableItem<*, *>>()

However I think I understand the problem, this should solve it.

val fastItemAdapter = FastItemAdapter<IItem<*, *>>()

for others after that you we will have to instance check and cast in onClick

fastItemAdapter.withOnClickListener { v, adapter, item, position -> // check instance of here and cast and perform actions }

Yes , this setup is working.

sid-dob avatar Nov 02 '18 13:11 sid-dob