java - RecyclerView崩溃

标签 java android android-recyclerview

我是 Android 和一般编程新手。我正在尝试加载 RecyclerView 一些数据。 在 Debug模式下,直到应用程序崩溃时才显示错误。 这里使用的主要数据是一个名为 Lettura 的实体,它是房间数据库的一部分。

这里我调用并用一些数据填充 View :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vedi_letture_simple);

        ArrayList<Lettura> letture = new ArrayList<>();

        Date date = new Date(1989, 1, 1);
        Lettura a = new Lettura("a", "b", "c", 14.5, date, 1);
        letture.add(a);

        RecyclerView recView = (RecyclerView) findViewById(R.id.lettura_recyclerview);
        LetturaAdapter mlAdapter = new LetturaAdapter(this, letture);  //made final
        recView.setAdapter(mlAdapter);
        // use a linear layout manager
        LinearLayoutManager lll = new LinearLayoutManager(this);
        recView.setLayoutManager(lll);

这里是适配器类LetturaAdapter.class

    class LetturaAdapter extends RecyclerView.Adapter<LetturaAdapter.LetturaViewHolder> {

    private ArrayList<Lettura> letture;  
    private LayoutInflater mInflater;

    public LetturaAdapter(Context context, ArrayList<Lettura> lettura){
        this.mInflater = LayoutInflater.from(context);
        this.letture = lettura;
    }
    @Override
    public LetturaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater mInflater = LayoutInflater.from(context);
        View viewRiga = mInflater.inflate(R.layout.lettura_recyclerview_element, parent, false);
        LetturaViewHolder rigaHolded = new LetturaViewHolder(viewRiga);
        return rigaHolded;
    }

    @Override
    public void onBindViewHolder(LetturaViewHolder rigaHolded, int position) {

        Lettura current;
        current = letture.get(position);

        TextView barT = hholded.barcodeItemView;
        barT.setText(current.getBarcode());
        TextView desT = hholded.descriptionItemView;
        desT.setText(current.getDescription());
        TextView priceT = hholded.priceItemView;
        priceT.setText(current.getPrice().toString());
        TextView dateT = hholded.dateItemView;
        dateT.setText(current.getDate().toString());
        TextView quanT = hholded.quantityItemView;
        quanT.setText(current.getQuantity());

    }

    @Override
    public int getItemCount() {
        if (letture != null)
            return letture.size();
        else return 0;
    }

    public class LetturaViewHolder extends RecyclerView.ViewHolder {
        TextView barcodeItemView;
        TextView descriptionItemView;
        TextView priceItemView;
        TextView dateItemView;
        TextView quantityItemView;

        public LetturaViewHolder(View itemView) {
            super(itemView);
            barcodeItemView = itemView.findViewById(R.id.barcodeTextView);
            descriptionItemView = itemView.findViewById(R.id.descriptionTextView);
            priceItemView = itemView.findViewById(R.id.priceTextView);
            dateItemView = itemView.findViewById(R.id.dateTextView);
            quantityItemView = itemView.findViewById(R.id.quantityTextView);
        }
}

这里是 Lettura 类

@Entity(tableName = "Lettura", indices = {@Index(value="barcode", unique=true)})// indices speeds the search
public class Lettura {

    @PrimaryKey(autoGenerate = true)
    public int id;
    @NonNull
    public String codNeg;
    @NonNull
    public String barcode;
    public String description;
    @NonNull
    public Double price;
    public Date date;
    public int quantity;
    public Lettura(String codNeg, String barcode, String description, Double price, Date date, int quantity) {
        this.codNeg = codNeg;
        this.barcode = barcode;
        this.description = description;
        this.price = price;
        this.date = date;
        this.quantity = quantity;
    }
    public String getCodNeg() {return this.codNeg;}
    public String getBarcode() {return this.barcode;}
    public String getDescription() {return this.description;}
    public Double getPrice() {return this.price;}
    public Date getDate() {return this.date;}
    public Integer getQuantity() {return this.quantity;}
}

这里是 RecyclerView 的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorScreenBackground">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/lettura_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

最后是元素的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <TextView
            android:id="@+id/barcodeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="1dp"
            android:layout_marginLeft="1dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:text="barcode"
            app:layout_constraintEnd_toEndOf="@+id/dateTextView"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/dateTextView" />

        <TextView
            android:id="@+id/descriptionTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="1dp"
            android:layout_marginLeft="1dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="1dp"
            android:layout_marginRight="1dp"
            android:layout_marginBottom="8dp"
            android:text="description"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/barcodeTextView"
            app:layout_constraintVertical_bias="0.0" />

        <TextView
            android:id="@+id/priceTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="price"
            app:layout_constraintEnd_toStartOf="@+id/dateTextView"
            app:layout_constraintTop_toTopOf="@+id/dateTextView" />

        <TextView
            android:id="@+id/dateTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:layout_marginEnd="1dp"
            android:layout_marginRight="1dp"
            android:text="date"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/quantityTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginEnd="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="8dp"
            android:text="quantity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/priceTextView"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/barcodeTextView"
            app:layout_constraintTop_toTopOf="@+id/dateTextView"
            app:layout_constraintVertical_bias="0.0" />

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_weight="1"
            android:background="#008AD4"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3" />
    </android.support.constraint.ConstraintLayout>

</LinearLayout>

最佳答案

我猜错误在这里:

TextView quanT = hholded.quantityItemView;
quanT.setText(current.getQuantity());

current.getQuantity() 返回一个 Integer 而不是 String。也许,你可以尝试:

TextView quanT = hholded.quantityItemView;
quanT.setText(current.getQuantity().toString());

关于java - RecyclerView崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56808128/

相关文章:

java - CDI Bean 的方法可以返回它创建的托管 Bean 吗?

java - jetty 不启动

android - 如何在启动服务时将 Intent 传递给它(但只是有时)

添加反向动画时 Android 动画显示空白 View

android - Recycler View 中的 OutOfMemory 错误

java - Android自定义复合组件的内容不会出现

android - 从 DPI(每英寸点数)android 获取以像素为单位的图像大小

java - 使用 Serializable 将数据从 RecyclerView 传递到 RecyclerViewMore - AndroidX

Android CoordinatorLayout : ToolBar scroll only when list starts scrolling

java - XML 解析和反序列化