Android 可以在xml中绑定两个变量吗?

Android 可以在xml中绑定两个变量吗?,android,android-databinding,Android,Android Databinding,此处销售点列表项目.xml <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"> <data> <variable n

此处销售点列表项目.xml

<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">

    <data>    
       <variable
            name="item"
            type="com.myproject.android.customer.api.model.Merchant" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="90dp"
            android:layout_height="90dp"/>

        <TextView
            android:id="@+id/phoneTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

    </android.support.constraint.ConstraintLayout>

</layout>
我使用方法
setVariable()
将变量“”与模型绑定

下面是具体的子适配器:

public class MapListSortAdapter extends PreviewSortAdapter {

    private static final String TAG = MapListSortAdapter.class.getName();

    public MapListSortAdapter(Context context, OrderedRealmCollection<Merchant> data) {
        super(context, data, true);
    }

    @Override
    protected int getLayoutForPosition(int position) {
        return R.layout.point_of_sale_list_item;
    }

}
这里是pojo代码:

public class PointOfSale {
    private int id;
    private String name;
    private String address;
    private String phone;
    private String email;

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }
}
但我当然会出错,因为第二个VARABLE()没有绑定:

java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.myproject.android.customer.api.model.PointOfSale.phone
file:\myProject\app\src\main\res\layout\point_of_sale_list_item.xml
loc:61:28 - 61:38
****\ data binding error ****
因此:

  • 可以绑定第二个变量吗
  • 如何从第二个变量获取数据

  • 或者创建一个新的模型来连接这两个模型?

    我肯定做到了。就我而言:

    <data>
        <variable name="viewModel" type="com.todtenkopf.myfrontpage.viewmodel.favoriteteam.FavoriteTeamVM"/>
        <variable name="matchup" type="com.todtenkopf.myfrontpage.viewmodel.favoriteteam.FavoriteTeamVM.Matchup"/>
    
    
    
    您不显示
    销售点的代码。
    phone
    是公共字段还是您有
    getPhone
    访问器?这听起来更像是问题所在。

    
    
       <data>   
          <variable
                    name="object name"
                    type="your pojo classpath" />
    
          <variable
                    name="handlers"
                    type="your handler class path " /> 
        </data>
    
    工作示例,下面共享链接

    你认为如果在
    销售点中有setter/getter方法,那么我可以在xml中使用2个变量?不,我是说我认为问题可能与2个变量无关。我使用了2个变量,没有问题。我猜想您看到的错误可能是因为
    phone
    不是公共字段,或者您没有公共
    getPhone
    getter。也许你可以发布
    销售点
    代码?请尝试将
    电话
    公开。或者更好,添加访问者
    getPhone
    。它无法绑定到
    手机
    ,因为它是私有的。它与两个变量无关。
    <data>
        <variable name="viewModel" type="com.todtenkopf.myfrontpage.viewmodel.favoriteteam.FavoriteTeamVM"/>
        <variable name="matchup" type="com.todtenkopf.myfrontpage.viewmodel.favoriteteam.FavoriteTeamVM.Matchup"/>
    
       <data>   
          <variable
                    name="object name"
                    type="your pojo classpath" />
    
          <variable
                    name="handlers"
                    type="your handler class path " /> 
        </data>