Android 可以在xml中绑定两个变量吗?
此处销售点列表项目.xmlAndroid 可以在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
<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>