Cannot set string array for spinner using Android data binding
Asked Answered
D

4

2

I have a spinner now and I want to load the data to the spinner with data binding feature. I tried to find the way to declare the string array in the XML(https://developer.android.com/topic/libraries/data-binding/index.html#expression_language) but no satisfied result is found.

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

<data>
    <import type="android.util.SparseArray"/>
    <variable
        name="statuslist"
        type="SparseArray<String>"/>
</data>
...
<android.support.v7.widget.AppCompatSpinner
        android:id="@+id/spn_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/spinner1"
        android:layout_below="@+id/btn2"
        android:entries="@{statuslist}"
        apps:adapter="@{statusAdapter}"/>
</layout>

When it is build, the following error is shown.

Error:Execution failed for task ':app:dataBindingProcessLayoutsDebug'.

org.xml.sax.SAXParseException; systemId: file:/C:/Users/../app/build/intermediates/res/merged/debug/layout/testdialog.xml; lineNumber: 24; columnNumber: 30; The value of attribute "type" associated with an element type "null" must not contain the '<' character.

However, there is something confused.

  1. When I tried to use SparseArray as shown on website. I found a "Cannot resolve sysmbol" in Android Studio XML Editor.
  2. I do not know is it the correct way to setup the array value for the spinner since there is no official instructions for the spinner data bindingin the website.
Dibble answered 9/8, 2016 at 3:6 Comment(2)
Have you enable databinding in build.gradle?Enphytotic
Yes, I enable databinding in build.gradle. Here is the information of my Android Studio (Version: 2.1.2, JavaVersion : 1.7)Dibble
P
0

have you import the SparseArray ?

try this :

<data><import type="android.util.SparseArray"/></data>
Pumpkin answered 9/8, 2016 at 3:57 Comment(7)
I forgot to add this statement in the question. I tried this but it doesn't workDibble
SparseArray<String> change to SparseArray&lt;String> , try againPumpkin
Tried this way and it still not work. "Cannot resolve symbol" error is still shownDibble
< is a reserved character in xml. Can you omit the type of the SparseArray. Not like it exists in the compiled code anyway.Woeful
What I meant is instead of SparseArray<String> write just SparseArray in the type field.Woeful
That's kind of weird , I remember when my Android Studio is V1.5 , it works, but now have the same problem , Android Studio is V2.1 , is that a bug or something ? I wonderPumpkin
@GPuschka, if I use the SparseArray without type , doesn't it mean this variable is not type safety? That would not be solved if I use the custom object (SparseArray<Customer>)Dibble
S
0

This is a very simple way to setup a spinner with a string-array from your resource's array.xml

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/spinner_array_items"/>

The spinner_array_items should contain a list of items to be found in the spinner dropdown.

Seismography answered 2/9, 2016 at 16:9 Comment(0)
A
0

Change SparseArray<String> to SparseArray&lt;String&gt;.

Apophyllite answered 21/8, 2018 at 8:36 Comment(0)
C
-1

Well my implementation logic is based on your's but used ArrayList instead SparseArray.

XML:

    <data>
        <import type="java.util.ArrayList" />
        <variable
            name="cities"
            type="ArrayList&lt;String>"
        />        
    </data>

    <!-- Location -->
    <Spinner
        android:id="@+id/dd_city"
        android:entries="@{cities}"
        style="@style/dropdown"
    />

Now, we just need to bind our ArrayList to cities variable through Binding Class.

At least data will be bind : ).

TC.

Commutative answered 5/2, 2019 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.