RecyclerView Spinner showing data for all the rows instead of only one row at a time

Viewed 52

I have two spinners in my RecyclerView. My first spinner is filled inside of the xml ->

<Spinner
       android:id="@+id/itemspnReason"
       android:layout_width="600dp"
       android:layout_height="match_parent"
       android:layout_gravity="center"
       android:entries="@array/list_reasons"
       />

How ever with my second spinner it is not so easy. The spinner will show a number between 1 and the quantity available per item showed in the RecyclerView. Currently I have this code inside my onBindViewHolder ->

val modal = partsList[position]
        holder.apply {
            var i = 1
            while (i <= modal.PQTY){ adapter.add(i.toString())
                i++}
            itemspnQTY.adapter = adapter
}

It works in getting to the desired number. The problem is each spinner is filled with all the data from each entry into the RecyclerView. For example if my first item had a size of 2 and my second item had a size of 4. Each of my spinner will contain the following: 1, 2, 1, 2, 3, 4.

So I need a way to clear the data after each entry or a better way to populate my spinner. My spinner is inside my item_list.xml for my RecyclerView. The easy solution is to do the same as my other spinner and create a static/hardcode entry. However I would like my spinner size to change based on how many items are available.

I have tried putting it inside my onCreateViewHolder and init. It did not solve problem.

1 Answers

I solved my problem. I found help from a different post and implemented it with my app. (How to make Dynamic Spinner inside RecyclerView?)

  override fun onBindViewHolder(holder: RowViewHolder, position: Int) {

        val modal = partsList[position]

         var i = 1
         val spnlist: MutableList<Int> = ArrayList()
   
             while (i <=  modal.PQTY){ spnlist.add(i)
                 i++}
         
         holder.itemspnQTY.adapter = 
                ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, spnlist)
}
Related