tiper / MaterialSpinner

Implementation of a Material Spinner for Android with TextInputLayout functionalities
Apache License 2.0
130 stars 30 forks source link

Spinner shows package name #37

Open mikelantzelo opened 3 years ago

mikelantzelo commented 3 years ago

Hi spinner shows package name when selecting an item. Can anyone help?

LunevNF commented 3 years ago

Same problem. Can customize the view for dropdown, but selected item - only package name and getView never called in adapter

AlfredAbdo commented 3 years ago

0- The MaterialSpinner library is an awesome idea, especially if you rely on the concept of Live Validation (using setError for TextInputLayout, etc.). That allows the use of MaterialSpinner without handling Android's Spinner, and also makes it look very similar to the other TextInputLayouts in a form page.

Same problem. Can customize the view for dropdown, but selected item - only package name and getView never called in adapter

1- My solution to the spinner selected item problem was to introduce a new interface for the items (not practical, but that was my bypass for using MaterialSpinner):

interface MaterialSpinnerItem {
    fun toSelectedString(): String
    fun toDisplayString(): String = toSelectedString()
}

Then, inside selection variable's setter (from line 127), add the MaterialSpinnerItem check for the 'editText.setText' clause:

editText.setText(
    when (val item = getItem(value) ?: "") {
        is CharSequence -> item
        is MaterialSpinnerItem -> item.toSelectedString()
        else -> item.toString()
    }
)

Then, feel free to use toDisplayString() in your custom adapter for the dropdown (or the other popups).

2- Since the MaterialSpinner is not technically an Android Spinner, the ListAdapter is not used as it would be used with a Spinner. Remember, MaterialSpinner is a TextInputLayout, i.e. it is not straight-forward to create a custom view as you would with a Spinner: the adapter is only used for the popups, where only the dropdown version is used (and where you need to use it).

I could not find a good solution for this as I am not sure a TextInputLayout (or even a TextInputEditText) allows to properly inflate views.