0
데이터 바인딩의 경우 Model model
에있는 멤버 변수 Tile[] tiles
을 XML 파일의 TextView
에 연결하려고합니다. (의 일부) 내 Model
클래스데이터 바인딩을 사용하여 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>
이는 다음과 같습니다 :
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);}
}
이것은
Error:(106, 33) Identifiers must have user defined types from the XML file. tiles is missing it
(의 일부) 내 XML : 그러나, 나는 다음과 같은 Gradle을 오류를 얻고있다 그리고 이것은 내 Tile
클래스 (의 일부)입니다 :
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;
}
}
내 타일 변수의 유형이 Model
이 아니기 때문에이 줄이 잘못 되었음 : type="com.myapp.Model"
,하지만 컴파일러/gradle은 com.myapp.Tile
도 com.myapp.Tile[]
도 com.myapp.Model.Tile[]
도 넣지 않습니다. 내 Tile[]
변수를 올바르게 선언하려면 어떻게해야합니까?
<variable
name="tiles"
type="com.<your package>.Tile[]"/> //it might show you an error in xml but you can ignore it.
및/뷰 모델
binding.setTiles(<your array>);
당신의 Tile
클래스와 그 변수가 공개되어 있는지 확인 활동/조각에서 그 값을 설정합니다
누군가가 묻기 전에 : dataBinding은 gradle에서 사용 가능합니다. – kalabalik