2017-10-23 23 views
0

データバインディングでは、Model modelにあるメンバー変数Tile[] tilesをXMLファイルのTextViewに接続しようとしています。しかし、私は次のGradleのエラーを取得しています:データバインディングを使用してオブジェクト配列をXMLで表現するにはどうすればよいですか?

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 &lt; 100 ? "48sp" : tiles[0].getValue &lt; 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 &lt; 100 ? "48sp" : tiles[1].getValue &lt; 1000 ? "36sp" : "24sp"}'/> 

     ... 

    </RelativeLayout> 
</layout> 

これは私のModelクラス(の一部)である:

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 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"、私のタイル変数の型は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. 

をして/のviewmodel

binding.setTiles(<your array>); 

あなたTileクラスとその変数が公開されていることを確認し活動/フラグメントからその値を設定します。

+0

誰かが尋ねる前に:dataBindingはgradleで有効になっています。 – kalabalik

答えて

2

あなたのXMLでこれを試してみてください。

public class Tile { 
    public int value; 
    public int id; 
    . 
    . 
    . 
} 
関連する問題