具有2向绑定的Android微调器设置选择 [英] Android Spinner Setting Selection with 2-Way Binding

查看:114
本文介绍了具有2向绑定的Android微调器设置选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在配置2向数据绑定时,我正在努力获得与Android微调器一起使用的某些功能。我想通过 android:selectedItemPosition 上的2-way数据绑定设置微调器的初始值。微调器条目由ViewModel初始化并正确填充,因此数据绑定似乎正常工作。



问题在于2向绑定 selectedItemPosition 。该变量已由ViewModel初始化为5,但微调框的选定项目保持为0(第一个项目)。调试时,似乎ObservableInt的值最初为5(已设置),但在executeBindings的第二阶段重置为零。



test_spinner_activity.xml

 < layout xmlns:tools = http://schemas.android.com/tools 
xmlns:android = http://schemas.android.com/apk/res/android
xmlns:app = http:// schemas.android.com/apk/res-auto\">

< data>
<变量名称= viewModel
type = com.aapp.viewmodel.TestSpinnerViewModel />
< / data>
< LinearLayout android:layout_width = match_parent
android:layout_height = wrap_content>
< android.support.v7.widget.AppCompatSpinner
android:layout_width = wrap_content
android:layout_height = match_parent
android:id = @ + id / sTimeHourSpinner
android:selectedItemPosition = @ = {viewModel.startHourIdx}
android:entries = @ {viewModel.startTimeHourSelections} />
< / LinearLayout>
< / layout>

TestSpinnerViewModel.java

 公共类TestSpinnerViewModel {
public final ObservableArrayList< String> startTimeHourSelections = new ObservableArrayList<>();
public final ObservableInt startHourIdx = new ObservableInt();

public TestSpinnerViewModel(Context context){
this.mContext = context;

for(int i = 0; i< 24; i ++){
int hour = i;
startTimeHourSelections.add(df.format(hour));
}
startHourIdx.set(5);
}
}

TestSpinnerActivity.java

 公共类TestSpinnerActivity扩展了AppCompatActivity {
私人TestSpinnerActivityBinding绑定;
private TestSpinnerViewModel mTestSpinnerViewModel;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

绑定= DataBindingUtil.bind(findViewById(R.id.test_spinner));
mTestSpinnerViewModel = new TestSpinnerViewModel(this);
binding.setViewModel(mTestSpinnerViewModel);
}

我正在使用Android Studio 2.2.2,并且启用了数据绑定。

解决方案

感谢您的建议。但是我找到了自己问题的答案。事实证明,将 android:selectedItemPosition = @ = {viewModel.startHourIdx} 变量从初始值5重置为0的原因是由于声明顺序 selectedItemPosition entries 属性的值。在我的示例中,它们以特定顺序声明,并且自动生成的绑定代码以相同顺序进行初始化。



因此,即使 selectedItemPosition 正确设置了条目的初始化导致ArrayAdapter的实例化,该适配器将 selectedItemPosition 重置为0。



因此,此修复程序是交换布局文件中的两个属性声明。

 < data> 
<变量名称= viewModel
type = com.aapp.viewmodel.TestSpinnerViewModel />
< / data>
< LinearLayout android:layout_width = match_parent
android:layout_height = wrap_content>
< android.support.v7.widget.AppCompatSpinner
android:layout_width = wrap_content
android:layout_height = match_parent
android:id = @ + id / sTimeHourSpinner
android:entries = @ {viewModel.startTimeHourSelections}
android:selectedItemPosition = @ = {viewModel.startHourIdx} />
< / LinearLayout>


I am struggling to get some functionality to work with Android spinners when configured with 2-way databinding. I would like to set the initial value of the spinner via the 2-way databinding on android:selectedItemPosition. The spinner entries are initialised by the ViewModel and are populated correctly, hence databinding appears to be working correctly.

The problem is with the 2-way binding of selectedItemPosition. The variable is initialised to 5 by the ViewModel but the spinner's selected item remains at 0 (the first item). When debugging it appears that the value of the ObservableInt is initially 5 (as set) but is reset to zero during the second phase of executeBindings.

Any help would be appreciated.

test_spinner_activity.xml

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="viewModel"
                  type="com.aapp.viewmodel.TestSpinnerViewModel"/>
    </data>
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content">
       <android.support.v7.widget.AppCompatSpinner
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/sTimeHourSpinner"
            android:selectedItemPosition="@={viewModel.startHourIdx}"
            android:entries="@{viewModel.startTimeHourSelections}"/>
    </LinearLayout>
</layout>

TestSpinnerViewModel.java

public class TestSpinnerViewModel {
    public final ObservableArrayList<String> startTimeHourSelections = new ObservableArrayList<>();
    public final ObservableInt startHourIdx = new ObservableInt();

    public TestSpinnerViewModel(Context context) {
        this.mContext = context;

        for (int i=0; i < 24; i++) {
            int hour = i;
            startTimeHourSelections.add(df.format(hour));
        }
        startHourIdx.set(5);
    }
}

TestSpinnerActivity.java

public class TestSpinnerActivity extends AppCompatActivity {
    private TestSpinnerActivityBinding binding;
    private TestSpinnerViewModel mTestSpinnerViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.bind(findViewById(R.id.test_spinner));
        mTestSpinnerViewModel = new TestSpinnerViewModel(this);
        binding.setViewModel(mTestSpinnerViewModel);
    }

I am using Android Studio 2.2.2 and Databinding is enabled.

解决方案

thank you for your suggestions. But I found the answer to my own question. It turns out that the reason that the android:selectedItemPosition=@={viewModel.startHourIdx} variable was being reset from the initialised value of 5 to 0 is because of the declaration order of the selectedItemPosition and entries attributes. In my example they were declared in that specific order and the auto-generated binding code produces initialisation in that same order.

Hence, even though the selectedItemPosition was set correctly the initialisation of the entries causes instantiation of the an ArrayAdapter which resets the selectedItemPosition to 0.

Hence, the fix is to swap the two attribute declarations in the layout file.

<data>
    <variable name="viewModel"
              type="com.aapp.viewmodel.TestSpinnerViewModel"/>
</data>
<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content">
   <android.support.v7.widget.AppCompatSpinner
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/sTimeHourSpinner"
        android:entries="@{viewModel.startTimeHourSelections}"
        android:selectedItemPosition="@={viewModel.startHourIdx}"/>
</LinearLayout>

这篇关于具有2向绑定的Android微调器设置选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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