我想制作一款类似于井字棋的安卓应用(不过是4x4网格)。我决定使用按钮来表示每个格子,并希望使用数据绑定将按钮上的文本绑定到一个String[][]数组中的数据,该数组将在内部表示网格。我尝试着做类似于这里介绍的内容:http://www.vogella.com/tutorials/AndroidDatabinding/article.html,于是我创建了这个类:
但是它给我返回这些错误:"元素data在此处不允许"以及"属性缺少android前缀"。从教程示例中无法确定我的错误所在,因此问题在于我应该把它们放在哪里?
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]}"/>
但是它给我返回这些错误:"元素data在此处不允许"以及"属性缺少android前缀"。从教程示例中无法确定我的错误所在,因此问题在于我应该把它们放在哪里?
"@={...}"
。请尝试使用"@{...}"
。 - George Mount"@={"
改为"@{"
似乎没有任何效果。尝试将其绑定到一个简单的字段而不是方法也没有起作用。我认为这可能与我正在使用的android.support.constraint.ConstraintLayout
布局有关,因为在我尝试遵循的教程和大多数其他教程中,它只是一个简单的layout
。 - FullStackOverflowDev