RecyclerView崩溃

时间:2019-06-28 13:45:28

标签: java android android-recyclerview

我是Android和编程领域的新手。我正在尝试向RecyclerView加载一些数据。 在调试模式下,直到应用崩溃时才显示错误。 这里使用的主要数据是一个称为Lettura的实体,它是会议室数据库的一部分。

在这里我调用并用一些数据填充视图:

    @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>

3 个答案:

答案 0 :(得分:1)

我猜错误在这里:

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

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

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

答案 1 :(得分:0)

尝试执行此操作-在 onBindViewHolder 中替换-

quanT.setText(current.getQuantity()); 

使用

quanT.setText(current.getQuantity()+"");

setText()方法仅接受字符串值,数量为整数值。因此,这里可能会崩溃。

答案 2 :(得分:0)

替换

quanT.setText(current.getQuantity()); 

使用

quanT.setText(String.valueOf(current.getQuantity()));

setText()仅适用于字符串。

Read more about converting Integer to String