MaterialSearchBar
MaterialSearchBar copied to clipboard
Cannot be cast to java.lang.String with custom suggestions adapter
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?
Did you find any solution?