mancj / MaterialSearchBar

Material Design Search Bar for Android
Other
2.03k stars 333 forks source link

Cannot be cast to java.lang.String with custom suggestions adapter #157

Open gfalasca opened 2 years ago

gfalasca commented 2 years ago

Hi, we use a custom suggestion adapter as in the example provided. Everything goes well most of the times, but we got some Cannot be cast to java.lang.String exceptions in production like the following:

Fatal Exception: java.lang.ClassCastException
  com.test.card.common.datamodel.catalog.Restaurants cannot be cast to java.lang.String
  com.mancj.materialsearchbar.adapter.DefaultSuggestionsAdapter.onBindSuggestionHolder (DefaultSuggestionsAdapter.java:17)
  com.mancj.materialsearchbar.adapter.SuggestionsAdapter.onBindViewHolder (SuggestionsAdapter.java:92)
  androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder (RecyclerView.java:7254)
  androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder (RecyclerView.java:7337)
  androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline (RecyclerView.java:6194)
  androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6460)
  androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:6300)
  androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:6296)
  androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2330)
  androidx.recyclerview.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1631)
  androidx.recyclerview.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1591)
  androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:668)
  ....

From the stack trace we see that the RecyclerView onBindViewHolder is referring to the DefaultSuggestionsAdapter class, not to our custom suggestion adapter we have set through the setCustomSuggestionAdapter as I was expecting.

Any idea on how to fix it? As I said the searchBar works correctly in many cases, but from some users we've got this exception. Should we refer to the custom suggestion adapter also from the layout xml of the searchbar? How?

alnahdy commented 1 year ago

Did you find any solution?