Android Spinner数据绑定与xml布局 [英] Android spinner data binding with xml layout

查看:622
本文介绍了Android Spinner数据绑定与xml布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataBinding类将数据从模型绑定到UI元素.我可以使用android:text="@{data.testData}"使绑定适用于EditText.这对于Spinner不起作用.从Pojo类获取数据并将其显示在微调器上的任何想法或解决方案(我有45个微调器)

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)

推荐答案

以下是我曾经做过的一个示例:

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

首先,您的布局xml(示例my_layout.xml):

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>

然后输入您的Java代码(MyLayoutBinding mBinding):

Then your java code (MyLayoutBinding mBinding):

adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, mData);
mBinding.setSpinnerAdapter(adapter);

这篇关于Android Spinner数据绑定与xml布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆