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)
-
1Having 45 spinners is a little bit odd. Have you considered other workarounds or designs ?Farhad– Farhad2016-01-23 10:19:02 +00:00Commented 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 nowVivian Ambrose– Vivian Ambrose2016-01-23 10:23:19 +00:00Commented Jan 23, 2016 at 10:23
-
2Thanks 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– Vivian Ambrose2016-01-23 10:46:17 +00:00Commented Jan 23, 2016 at 10:46
-
nice thing to learn today :). thanks anyway. way to go :)Farhad– Farhad2016-01-23 11:04:07 +00:00Commented Jan 23, 2016 at 11:04
-
@Vivian Ambrose, hey how do you initialize your spinner adapters? Is it through binding too?WindRider– WindRider2016-06-15 17:21:49 +00:00Commented Jun 15, 2016 at 17:21
Add a comment
|
2 Answers
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);
4 Comments
Hong
Where is app:adapter attribute defined?
maohieng
It's not defined anywhere because data-binding will find setter method
setAdapter() by itself when it meets app:adapter="@{spinnerAdapter}" like this.CACuzcatlan
Can you provide the code for the entire class where the adapter lives (MyLayoutBidning mBinding)? Thanks.
maohieng
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.htmlMy 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
James
I get a warning
Unknown attribute android:selectedItemPosition but it still seems to work