Android Studio 数据绑定中不允许使用元素数据
Element data is not allowed here in Android Studio data binding
我正在尝试制作一种棋盘游戏 android 应用程序(尽管使用 4x4 网格)。我决定使用按钮来描绘每个网格方块,并希望使用数据绑定将按钮上的文本绑定到 String[][] 数组中的数据,该数组将在内部表示网格。我尝试做一些类似于这里介绍的事情 http://www.vogella.com/tutorials/AndroidDatabinding/article.html 所以我创建了这个 class:
public class ModelJoc extends BaseObservable{
private String[][] tabla_joc;
public ModelJoc() {
for (String[] line : tabla_joc)
for (String element : line)
element = "";
tabla_joc[0][0] = "M";
tabla_joc[0][1] = "W";
}
然后将数据绑定添加到activity_main.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
然后尝试将按钮的文本设置为数组中的值:
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="@={state.getBlockState()[0][0]}"/>
但它给了我这些错误:"Element data is not allowed here" 以及 "Attribute is missing the android: prefix"。我无法从教程中的示例中判断出我做错了什么,所以问题是我应该把它们放在哪里?
我想我现在明白问题所在了。您必须使用 <layout>
标记勾勒您的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ... -->
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="@{state.getBlockState()[0][0]}"/>
如果不这样做,android 数据绑定将不会将其识别为数据绑定布局文件。
我正在尝试制作一种棋盘游戏 android 应用程序(尽管使用 4x4 网格)。我决定使用按钮来描绘每个网格方块,并希望使用数据绑定将按钮上的文本绑定到 String[][] 数组中的数据,该数组将在内部表示网格。我尝试做一些类似于这里介绍的事情 http://www.vogella.com/tutorials/AndroidDatabinding/article.html 所以我创建了这个 class:
public class ModelJoc extends BaseObservable{
private String[][] tabla_joc;
public ModelJoc() {
for (String[] line : tabla_joc)
for (String element : line)
element = "";
tabla_joc[0][0] = "M";
tabla_joc[0][1] = "W";
}
然后将数据绑定添加到activity_main.xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
然后尝试将按钮的文本设置为数组中的值:
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="@={state.getBlockState()[0][0]}"/>
但它给了我这些错误:"Element data is not allowed here" 以及 "Attribute is missing the android: prefix"。我无法从教程中的示例中判断出我做错了什么,所以问题是我应该把它们放在哪里?
我想我现在明白问题所在了。您必须使用 <layout>
标记勾勒您的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ... -->
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="@{state.getBlockState()[0][0]}"/>
如果不这样做,android 数据绑定将不会将其识别为数据绑定布局文件。