使用数据绑定后,当我运行应用程序时出现错误

时间:2018-10-04 05:40:56

标签: android mvvm data-binding

这是我遇到的错误,

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
  

android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。     **** /数据绑定错误**** msg:找不到访问器com.rk.mymvvmdemo.viewmodel.OnlineRetailersViewModel.name     文件:D:\ All Demo \ MVVM Demo by Rakesh \ app \ src \ main \ res \ layout \ custom_online_retailer_item.xml     loc:33:36-33:63     **** \数据绑定错误****

这里是零售商的Adapter类

    public class OnlineRetailersAdapter extends RecyclerView.Adapter<OnlineRetailersAdapter.OnlineRetailesViewHolder>
{
    private Context context;
    private List<OnlineRetailersModel> retailersModelList;

    public OnlineRetailersAdapter(Context context, List<OnlineRetailersModel> retailersModelList) {
        this.context = context;
        this.retailersModelList = retailersModelList;
    }


    @Override
    public OnlineRetailesViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        CustomOnlineRetailerItemBinding customOnlineRetailerItemBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_online_retailer_item, parent, false);
        return new OnlineRetailesViewHolder(customOnlineRetailerItemBinding);
    }

    @Override
    public void onBindViewHolder(OnlineRetailesViewHolder holder, int position) {
        holder.bindOnlineRetailers(retailersModelList.get(position));
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    static class OnlineRetailesViewHolder extends RecyclerView.ViewHolder
    {
        private CustomOnlineRetailerItemBinding customOnlineRetailerItemBinding;
        OnlineRetailesViewHolder(CustomOnlineRetailerItemBinding customOnlineRetailerItemBinding)
        {
            super(customOnlineRetailerItemBinding.retailerCard);
            this.customOnlineRetailerItemBinding = customOnlineRetailerItemBinding;
        }

        void bindOnlineRetailers(OnlineRetailersModel OnlineRetailersModel)
        {
            if (customOnlineRetailerItemBinding.getOnlineRetalersItemModel() == null) {
                customOnlineRetailerItemBinding.setOnlineRetalersItemModel(new OnlineRetailersViewModel(OnlineRetailersModel, itemView.getContext()));
            } else {
                customOnlineRetailerItemBinding.getOnlineRetalersItemModel().setRetailers(OnlineRetailersModel);
            }
        }
    }
}

这是OnlineRetailerViewModel类

public class OnlineRetailersViewModel extends BaseObservable
{
    Context context;
    String name, url, description;

    public OnlineRetailersViewModel(OnlineRetailersModel onlineRetailersModel, Context context)
    {
        this.name = onlineRetailersModel.getName();
        this.url = onlineRetailersModel.getUrl();
        this.description = onlineRetailersModel.getDescription();
        this.context = context;
    }

    public void setRetailers(OnlineRetailersModel onlineRetailersModel) {
        this.name = onlineRetailersModel.getName();
        this.url = onlineRetailersModel.getUrl();
        this.description = onlineRetailersModel.getDescription();
        notifyChange();
    }

}

这是custom_online_retailer_item xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
    <data>
        <variable
            name="onlineRetalersItemModel"
            type="com.rk.mymvvmdemo.viewmodel.OnlineRetailersViewModel"/>
    </data>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/retailer_card"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:cardUseCompatPadding="true"
        app:cardCornerRadius="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="10dp"
                android:id="@+id/retailer_image"
                android:src="@mipmap/ic_launcher"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@{onlineRetalersItemModel.name}"
                    android:textColor="@color/primaryTextColor"
                    android:id="@+id/retailer_name" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:autoLink="web"
                    android:text="@{onlineRetalersItemModel.url}"
                    android:id="@+id/retailer_url"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@{onlineRetalersItemModel.description}"
                    android:id="@+id/retailer_description"/>
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</layout>

任何人都可以在我做错事情的地方提供帮助。

0 个答案:

没有答案