错误:找不到符号类CouponBindingImpl

时间:2018-12-31 01:12:07

标签: java android android-studio android-recyclerview android-databinding

我正在尝试制作一个可向用户显示优惠券列表的Android应用程序,供用户使用RecyclerView选择。我正在尝试使用DataBinding将优惠券的详细信息绑定到优惠券XML模板,然后将那些优惠券绑定到RecyclerView

当我尝试构建时,我不断出错:

  

找不到符号类CouponItemBindingImpl

这是我的优惠券项目模板的XML代码:

   
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.v7.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    app:cardBackgroundColor="@android:color/transparent"
    app:cardCornerRadius="14dp"
    app:cardElevation="6dp">


    <LinearLayout
        android:id="@+id/linearLayoutHorizontal"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <data xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            class="CouponItem">
            <import type="com.example.mmcke.campuscoupons.model.Coupon"/>

            <variable
                name="data"
                type="Coupon" />
            <import type="com.example.mmcke.campuscoupons.R"/>
        </data>

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{data.name}"
                android:textSize="24sp" />

            <TextView
                android:id="@+id/details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@{data.details}"
                android:textSize="14sp" />

        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>
</layout>

这是我主要活动的XML代码,该屏幕包含RecyclerView

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>
    <import type="com.example.mmcke.campuscoupons.model.Coupon"/>
    <variable
        name="data"
        type="Coupon"/>

</data>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mmcke.campuscoupons.controllers.MainActivity">

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Campus Coupons"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="368dp"
        android:layout_height="455dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager">
    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

</layout>

这是我的主要活动的Java代码:

package com.example.mmcke.campuscoupons.controllers;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.mmcke.campuscoupons.BR;
import com.example.mmcke.campuscoupons.R;
import com.example.mmcke.campuscoupons.databinding.ActivityMainBinding;
import com.example.mmcke.campuscoupons.model.Coupon;
import com.example.mmcke.campuscoupons.model.CouponAdapter;
import com.example.mmcke.campuscoupons.model.Model;
import com.example.mmcke.campuscoupons.model.School;
import com.example.mmcke.campuscoupons.databinding.CouponItemBinding;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private final Model model = Model.getInstance();
private ArrayList<Coupon> coupons = new ArrayList<Coupon>();
private ActivityMainBinding binding;
private RecyclerView recyclerView;
private CouponAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    coupons = getEnumFromString(School.class, model.getCurrentUser().getSchoolName()).getCoupons();
    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    adapter = new CouponAdapter(this, coupons);
    recyclerView.setAdapter(adapter);

}

@BindingAdapter({"entries", "layout"})
public static <Coupon> void setEntries(ViewGroup viewGroup, ArrayList<Coupon> entries, int layoutID) {
    if (entries != null) {
        LayoutInflater inflater = (LayoutInflater)viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < entries.size(); i++) {
            Coupon entry = entries.get(i);
            ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutID, viewGroup, true);
            //binding.setVariable(BR.data, entry);
        }
    }
}

public static <School extends Enum<School>> School getEnumFromString (Class<School> c, String string) {
    if (c != null && string != null) {
        try {
            return Enum.valueOf(c, string.trim().toUpperCase());
        } catch (IllegalArgumentException ex) {

        }
    }
    return null;
}
}

这是我为优惠券项目制作的持有人:

package com.example.mmcke.campuscoupons.model;

import android.content.ClipData;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mmcke.campuscoupons.BR;
import com.example.mmcke.campuscoupons.R;
import com.example.mmcke.campuscoupons.databinding.ActivityMainBinding;
import com.example.mmcke.campuscoupons.databinding.CouponItemBinding;


/**
 *
 * A class to help with displaying coupons
 * Created by mmcke on 11/21/2018.
 */

public class CouponHolder extends RecyclerView.ViewHolder {

final ViewDataBinding binding;
//private final CouponItemBinding binding;

public CouponHolder(CouponItemBinding binding) {
    super(binding.getRoot());
    this.binding = binding;
}

public void bind(Object obj) {
    binding.setVariable(BR.data, obj);
    binding.executePendingBindings();
}
}

最后,这是我为优惠券项目制作的适配器:

package com.example.mmcke.campuscoupons.model;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.mmcke.campuscoupons.BR;
import com.example.mmcke.campuscoupons.R;
import com.example.mmcke.campuscoupons.databinding.ActivityMainBinding;
import com.example.mmcke.campuscoupons.databinding.CouponItemBinding;

import java.util.ArrayList;

/**
 *
 * A class to help with displaying coupons
 * Created by mmcke on 11/21/2018.
 */

public class CouponAdapter extends RecyclerView.Adapter<CouponHolder>{

private Context context;
private ArrayList<Coupon> coupons;

public CouponAdapter(Context _context, ArrayList<Coupon> _coupons) {
    this.context = _context;
    this.coupons = _coupons;
}

@Override
public int getItemCount() {
    return coupons.size();
}

@Override
public CouponHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    CouponItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.coupon_item, parent, false);
    return new CouponHolder(binding);
}

@Override
public void onBindViewHolder(CouponHolder holder, int position) {
    Coupon data = coupons.get(position);
    holder.binding.setVariable(BR.data, data);
}

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您还应该将 data 标记放入Coupon Item的XML代码中,并为CouponItem设置viewModel类。然后,编译器可以创建CouponItemBindingImpl类。因此,尝试如下更新您的coupon_item.xml文件;

<layout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="viewModel"
            type="path.to.your.viewmodel.class"/>
    </data>

    ... // your layout
</layout>