arcadefire / nice-spinner

A nice spinner for Android
Apache License 2.0
2.85k stars 446 forks source link

setSelectedIndex crash #166

Open npabion opened 4 years ago

npabion commented 4 years ago

If the spinner uses a selected text formatter with a type, calling setSelectedIndex crashes:

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Locale
        ...
        at org.angmarch.views.NiceSpinner.setTextInternal(NiceSpinner.java:307)
        at org.angmarch.views.NiceSpinner.setSelectedIndex(NiceSpinner.java:323)

Example:

val textFormatter: SpinnerTextFormatter<Locale> = SpinnerTextFormatter<Locale> {
            locale -> SpannableString(locale.getDisplayName(defaultLocale))
}

view.locale_spinner.setSpinnerTextFormatter(textFormatter)
view.locale_spinner.setSelectedTextFormatter(textFormatter)
view.locale_spinner.attachDataSource(locales)
view.locale_spinner.selectedIndex = someIndex

The problem is setSelectedIndex calls setTextInternal(selectedTextFormatter.format(adapter.getItemInDataset(position)).toString()); which then calls setText(selectedTextFormatter.format(item)); but item is already a string and not the expected type.

nocrisis commented 3 years ago

METOO

fanqGithub commented 3 years ago

代码下下来,修改下setTextInternal

nocrisis commented 3 years ago

代码下下来,修改下setTextInternal

修改成什么