.BusinessListAdapter cannot be cast to android.widget.HeaderViewListAdapter

1.5k Views Asked by At

I'm trying to convert between ListAdapter and HeaderViewListAdapter And when I run my app I get the following error:

Caused by: java.lang.ClassCastException: com.trustme.ui.adapter.BusinessListAdapter cannot be cast to android.widget.HeaderViewListAdapter

this is my code:

HeaderViewListAdapter hlva = ((HeaderViewListAdapter) searchBy.getPtr()
            .getAdapter());
2

There are 2 best solutions below

0
Marcin Orlowski On

You cannot do that. the only thing they got in common is word "Adapter" in name. And that's it. HeaderViewListAdapter does not implement Adapter, not to mention ListAdapter is interface.

0
Peter On

Looking into the API - documentation you will find that a HeaderViewListAdapter implements WrapperListAdapter which is a extension of the ListAdapter - interface. But if BusinessListAdapter is a ListAdapter and you are trying to cast it to HeaderViewListAdapter this is an upcast, which won't work unless the underlying object of the BusinessListAdapter is a WrapperListAdapter or a HeaderViewListAdapter, say:

BusinessListAdapter adapter = new HeaderViewListAdapter();

HeaderViewListAdapter adapter2 = (HeaderViewListAdapter) adapter;