如何使用数据绑定在 XML 中表示对象数组?
How do I represent an Object Array in XML with data binding?
通过数据绑定,我试图将驻留在 Model model
中的成员变量 Tile[] tiles
连接到 XML 文件中的 TextView
。但是,我收到以下 gradle 错误:
Error:(106, 33) Identifiers must have user defined types from the XML file. tiles is missing it
这是我的(部分)XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="(Tile[]) tiles"
type="com.myapp.Model" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv0"
android:layout_width="100dp"
android:layout_height="100dp"
android:text='@{tiles[0].getValue == 0 ? "" : tiles[0].getValue}'
android:textSize='@{tiles[0].getValue < 100 ? "48sp" : tiles[0].getValue < 1000 ? "36sp" : "24sp"}'/>
<TextView
android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text='@{tiles[1].getValue == 0 ? "" : tiles[1].getValue}'
android:textSize='@{tiles[1].getValue < 100 ? "48sp" : tiles[1].getValue < 1000 ? "36sp" : "24sp"}'/>
...
</RelativeLayout>
</layout>
这是我的(部分)Model
class:
class Model {
private Tile[] tiles = new Tile[16];
Model(Tile[] tiles) {
setTiles(tiles);
}
Tile[] getTiles(){ return tiles;}
void setTiles(Tile[] tiles) { System.arraycopy(tiles, 0, this.tiles, 0, tiles.length); }
int getValue(int index) { return tiles[index].getValue(); }
void setValue(int index, int value) { tiles[index].setValue(value); }
int getId(int index) { return tiles[index].getId(); }
void setId(int index, int id) { tiles[index].setId(id);}
}
这是我的(部分)Tile
class:
class Tile {
private int value;
private int id;
Tile(int id, int value) {
this.id = id;
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
int getId() {
return id;
}
void setId(int id) {
this.id = id;
}
}
这行很可能是错误的:type="com.myapp.Model"
,因为我的 tiles 变量的类型不是 Model
,但是 compiler/gradle 不允许我输入 com.myapp.Tile
] 也不 com.myapp.Tile[]
也不 com.myapp.Model.Tile[]
。如何正确声明我的 Tile[]
变量?
在你的 xml 中试试这个:
<variable
name="tiles"
type="com.<your package>.Tile[]"/> //it might show you an error in xml but you can ignore it.
并根据 activity/fragment/viewmodel
设置其值
binding.setTiles(<your array>);
确保您的 Tile
class 及其变量是 public.
public class Tile {
public int value;
public int id;
.
.
.
}
通过数据绑定,我试图将驻留在 Model model
中的成员变量 Tile[] tiles
连接到 XML 文件中的 TextView
。但是,我收到以下 gradle 错误:
Error:(106, 33) Identifiers must have user defined types from the XML file. tiles is missing it
这是我的(部分)XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="(Tile[]) tiles"
type="com.myapp.Model" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv0"
android:layout_width="100dp"
android:layout_height="100dp"
android:text='@{tiles[0].getValue == 0 ? "" : tiles[0].getValue}'
android:textSize='@{tiles[0].getValue < 100 ? "48sp" : tiles[0].getValue < 1000 ? "36sp" : "24sp"}'/>
<TextView
android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text='@{tiles[1].getValue == 0 ? "" : tiles[1].getValue}'
android:textSize='@{tiles[1].getValue < 100 ? "48sp" : tiles[1].getValue < 1000 ? "36sp" : "24sp"}'/>
...
</RelativeLayout>
</layout>
这是我的(部分)Model
class:
class Model {
private Tile[] tiles = new Tile[16];
Model(Tile[] tiles) {
setTiles(tiles);
}
Tile[] getTiles(){ return tiles;}
void setTiles(Tile[] tiles) { System.arraycopy(tiles, 0, this.tiles, 0, tiles.length); }
int getValue(int index) { return tiles[index].getValue(); }
void setValue(int index, int value) { tiles[index].setValue(value); }
int getId(int index) { return tiles[index].getId(); }
void setId(int index, int id) { tiles[index].setId(id);}
}
这是我的(部分)Tile
class:
class Tile {
private int value;
private int id;
Tile(int id, int value) {
this.id = id;
this.value = value;
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
int getId() {
return id;
}
void setId(int id) {
this.id = id;
}
}
这行很可能是错误的:type="com.myapp.Model"
,因为我的 tiles 变量的类型不是 Model
,但是 compiler/gradle 不允许我输入 com.myapp.Tile
] 也不 com.myapp.Tile[]
也不 com.myapp.Model.Tile[]
。如何正确声明我的 Tile[]
变量?
在你的 xml 中试试这个:
<variable
name="tiles"
type="com.<your package>.Tile[]"/> //it might show you an error in xml but you can ignore it.
并根据 activity/fragment/viewmodel
设置其值binding.setTiles(<your array>);
确保您的 Tile
class 及其变量是 public.
public class Tile {
public int value;
public int id;
.
.
.
}