mancj / mancj/MaterialSearchBar

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

Open
#157 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2k
Forks
324
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing the binding path through DefaultSuggestionsAdapter.java and SuggestionsAdapter.java, then compare it with the custom adapter configured through setCustomSuggestionAdapter. Reproduce the failure with a non-String suggestion such as the Restaurants object and inspect whether the layout XML or adapter selection bypasses the custom adapter. Done means custom suggestions bind without the ClassCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.