Android Spinner Setting Selection with 2-Way Binding
<p>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 <code>android:selectedItemPosition</code>. The spinner entries are initialised by the ViewModel and are populated correctly, hence databinding appears to be working correctly.</p>

<p>The problem is with the 2-way binding of <code>selectedItemPosition</code>. 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.</p>

<p>Any help would be appreciated.</p>

<p>test_spinner_activity.xml</p>

<pre><code>&lt;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"&gt;

    &lt;data&gt;
        &lt;variable name="viewModel"
                  type="com.aapp.viewmodel.TestSpinnerViewModel"/&gt;
    &lt;/data&gt;
    &lt;LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"&gt;
       &lt;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}"/&gt;
    &lt;/LinearLayout&gt;
&lt;/layout&gt;
</code></pre>

<p>TestSpinnerViewModel.java</p>

<pre><code>public class TestSpinnerViewModel {
    public final ObservableArrayList&lt;String&gt; startTimeHourSelections = new ObservableArrayList&lt;&gt;();
    public final ObservableInt startHourIdx = new ObservableInt();

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

        for (int i=0; i &lt; 24; i++) {
            int hour = i;
            startTimeHourSelections.add(df.format(hour));
        }
        startHourIdx.set(5);
    }
}
</code></pre>

<p>TestSpinnerActivity.java</p>

<pre><code>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);
    }
</code></pre>

<p>I am using Android Studio 2.2.2 and Databinding is enabled.</p>