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)

5
  • 1
    Having 45 spinners is a little bit odd. Have you considered other workarounds or designs ? Commented Jan 23, 2016 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 Commented Jan 23, 2016 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() Commented Jan 23, 2016 at 10:46
  • nice thing to learn today :). thanks anyway. way to go :) Commented Jan 23, 2016 at 11:04
  • @Vivian Ambrose, hey how do you initialize your spinner adapters? Is it through binding too? Commented Jun 15, 2016 at 17:21

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Where is app:adapter attribute defined?
It's not defined anywhere because data-binding will find setter method setAdapter() by itself when it meets app:adapter="@{spinnerAdapter}" like this.
Can you provide the code for the entire class where the adapter lives (MyLayoutBidning mBinding)? Thanks.
You mean the entire class of MyLayoutBinding? It is generated by Android Data Binding when you specify <layout> tag in your xml layout (my_layout.xml). See developer.android.com/topic/libraries/data-binding/index.html
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

        })

1 Comment

I get a warning Unknown attribute android:selectedItemPosition but it still seems to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.