11

I'm using DataBinding class to bind data from the model to the UI elements. I could get the binding working for an EditText using android:text="@{data.testData}". This somehow doesn't work for a Spinner. Any ideas or solutions to get the data from the Pojo class and display it on a spinner (I have 45 spinners)

starkshang
  • 8,228
  • 6
  • 41
  • 52
Vivian Ambrose
  • 111
  • 1
  • 1
  • 3
  • 1
    Having 45 spinners is a little bit odd. Have you considered other workarounds or designs ? – Farhad Jan 23 '16 at 10:19
  • Yes it's a survey form and binded one way using Android Form Enhancer(ui to model), I need to just get the other way working now – Vivian Ambrose Jan 23 '16 at 10:23
  • 2
    Thanks for having a look at this question, I'm happy to say that I have found the solution for this. For a spinner we need to use app:selection="@{data.testSpinnerData}" and it's compulsory to have a getter method in your Pojo class that would return ONLY an integer value. eg. public int getTestSpinnerData() – Vivian Ambrose Jan 23 '16 at 10:46
  • nice thing to learn today :). thanks anyway. way to go :) – Farhad Jan 23 '16 at 11:04
  • @Vivian Ambrose, hey how do you initialize your spinner adapters? Is it through binding too? – WindRider Jun 15 '16 at 17:21

2 Answers2

9

Here is an example, that I used to do it:

First, your layout xml (example my_layout.xml):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="spinnerAdapter"
            type="android.widget.ArrayAdapter" />
    </data>

...

    <android.support.v7.widget.AppCompatSpinner
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:adapter="@{spinnerAdapter}" />

....
</layout>

Then your java code (MyLayoutBinding mBinding):

adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, mData);
mBinding.setSpinnerAdapter(adapter);
maohieng
  • 1,646
  • 1
  • 19
  • 26
  • Where is app:adapter attribute defined? – Hong Mar 13 '17 at 19:12
  • 1
    It's not defined anywhere because data-binding will find setter method `setAdapter()` by itself when it meets `app:adapter="@{spinnerAdapter}"` like this. – maohieng Mar 14 '17 at 04:50
  • Can you provide the code for the entire class where the adapter lives (MyLayoutBidning mBinding)? Thanks. – CACuzcatlan Mar 28 '17 at 00:01
  • You mean the entire class of `MyLayoutBinding`? It is generated by Android Data Binding when you specify `` tag in your xml layout (`my_layout.xml`). See https://developer.android.com/topic/libraries/data-binding/index.html – maohieng Mar 28 '17 at 10:05
3

My solution: XML:

  <androidx.appcompat.widget.AppCompatSpinner
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/button_background"
                android:entries="@{viewModel.items}"
                android:padding="5dp"
                android:selectedItemPosition="@{viewModel.itemPosition}" />

ViewModel:

  var items: ArrayList<Item> = ArrayList()
  var itemPosition = MutableLiveData<Int>()

Activity/Fragment:

   viewModel.itemPosition.observe(requireActivity(),
        object : Observer<Int> {
            override fun onChanged(t: Int?) {
                //you will get the position on selection os spinner
                //get value by position

        })
Kerberos
  • 4,036
  • 3
  • 36
  • 55
creativecoder
  • 1,470
  • 1
  • 14
  • 23
  • 1
    I get a warning `Unknown attribute android:selectedItemPosition` but it still seems to work – James Nov 03 '20 at 12:38