I've an enum class Product, that has enums listed as well as a companion object method to return a list.
@Parcelize
enum class Product: Parcelable {
FOO,
BAR,
BAZ;
companion object {
fun list(): ArrayList<Product> {
return arrayListOf(FOO, BAR, BAZ)
}
}
}
In the layout.xml I have a spinner with an import statement. I'm binding the list function with the spinner entries.
<layout>
<data>
<import type="data.Product" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout>
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:entries="@{Product.Companion.list()}" />
Problem: Can't build or compile the app.
Could not find accessor
data.Product.Companion.comboList
Edit 1
I got one step closer with the help of this article.
Changed import to a variable and included Companion object
<data>
<variable name="productStatic" type="data.Product.Companion" />
</data>
and spinner as
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:entries="@{ProductStatic.list}" />
FormFragment is as (I'm not clear here, how to handle here?), spinner list is still empty.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = UserFormBinding.inflate(inflater, container, false).apply {
productStatic.list = Product.list()
}
return binding.root
}
Edit 2
From the comments, I realized I was doing it wrong, it should have been through an adapter from kotlin code. Here's my update but the question is how do I auto select it?
Layout xml
<data>
<variable name="products" type="android.widget.ArrayAdapter" />
<variable name="user" type="data.User" />
</data>
...
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adapter="@{products}" />
FormFragment.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = UserFormBinding.inflate(inflater, container, false).apply {
user = this@UserForm.user
products = ArrayAdapter(activity!!, android.R.layout.simple_spinner_item, Product.list())
}
return binding.root
}