여기 point_of_sale_list_item.xml 여기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>
루트 어댑터 :
public abstract class PreviewSortAdapter extends RealmRecyclerViewAdapter {
public PreviewSortAdapter(Context context, @Nullable OrderedRealmCollection data, boolean autoUpdate) {
super(data, true);
setHasStableIds(true);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, viewType, parent, false);
setClickHandler(binding);
return new PreviewBaseViewHolder(binding);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Object obj = getItem(position);
PreviewBaseViewHolder previewViewHolder = (PreviewBaseViewHolder) holder;
ViewDataBinding viewDataBinding = previewViewHolder.getBinding();
viewDataBinding.setVariable(BR.item, obj);
viewDataBinding.executePendingBindings(); //binding must be executed immediately
}
@Override
public int getItemViewType(int position) {
return getLayoutForPosition(position);
}
protected abstract int getLayoutForPosition(int position);
private class PreviewBaseViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;
private PreviewBaseViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
private ViewDataBinding getBinding() {
return binding;
}
}
}
나는 모델과 변수 "항목"를 결합하는 방법 setVariable()
를 사용합니다.
그리고 여기에 콘크리트 아이 어댑터 :
OK point_of_sale_list_item :
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;
}
}
이 I 어댑터에서 나는 단지 레이아웃 XML을 지정합니다. 모두 잘 작동합니다.
하지만 이제 다른 모델에서 설정된 전화 번호를 읽어야합니다. ID의 xml android:[email protected]+id/phoneTextView
따라서 두 번째 변수를 바인딩해야합니다. 이 같은 뭔가 :
<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" />
<variable
name="point"
type="com.myproject.android.customer.api.model.PointOfSale" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="90dp"
android:layout_height="90dp" />
<TextView
android:id="@+id/phoneTextView"
android:layout_width="0dp"
android:text="@{point.phone}"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
</layout>
여기에 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;
}
}
그러나 물론 내가 얻을 오류, 두 번째 vairable (점)이 결합되지 않았기 때문에 : 그래서
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 ****
:
- 두 번째를 바인딩 할 수 있습니까?
- 어떻게 두 번째 변수에서 데이터를 가져올 수 있습니까?
아니면이 두 모델을 연결하는 새 모델을 만드시겠습니까?
'PointOfSale'에 setter/getter 메소드가있는 경우 xml에 2 개의 변수를 사용할 수 있다고 주장합니까? – Alex
아니, 문제가 2 개의 변수와 관련이 없다고 생각한다고 말하고 있습니다. 나는 문제없이 2 개의 변수를 사용했다. 나는'phone'이 public 필드가 아니거나 public getPhone getter가 없기 때문에 여러분이 보았던 오류가있을 것이라고 추측했다. 아마도'PointOfSale' 코드를 게시 할 수 있습니까? –
내 게시물을 업데이트합니다. – Alex