0

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
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
AppDeveloper
  • 1,816
  • 7
  • 24
  • 49
  • Any good reason to not do it from kotlin code using an ArrayAdapter ? – because_im_batman Apr 01 '20 at 06:32
  • I finally realized, a while back, let me do Edit 2, I fixed it but the dilema, how do I make it autoselect? – AppDeveloper Apr 01 '20 at 06:35
  • Please see my edits, how to I make it autoselect from `user` – AppDeveloper Apr 01 '20 at 06:38
  • @because_im_batman `Any good reason to not do it from kotlin code using an ArrayAdapter ` so it's not possible with my earlier approach? just curious and wondering. – AppDeveloper Apr 01 '20 at 06:54
  • Its possible, but i would never want to do it like this personally. Since, its so much nicer and cleaner 2-3 lines of code in kotlin. Also, referring to class names from xml can be tedious, specially when u are applying proguard also one can easily want to refactor/rename their classes. In case you still need the code for doing it from kotlin in a few lines, tell me i'll answer. – because_im_batman Apr 01 '20 at 07:01
  • @because_im_batman thanks for the explanation, I understand it's better to do it in Kotlin code which I followed in my Edit 2, Can you please enlighten me on how can it be done in the context of Edit 1, i.e. class names in the XML, I'm still not fulfilled there knowledge-wise :) – AppDeveloper Apr 01 '20 at 08:23
  • can you try removing the "private" modifier in your list() method. ? – because_im_batman Apr 01 '20 at 13:17
  • I did, actually i changed it earlier but problem is persisting, i can't populate the spinner. – AppDeveloper Apr 01 '20 at 20:37

0 Answers0