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

      LinearLayoutManagr 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 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());


    }






噜噜哒
浏览 141回答 3
3回答

POPMUISE

我想错误在这里:TextView&nbsp;quanT&nbsp;=&nbsp;hholded.quantityItemView; quanT.setText(current.getQuantity());current.getQuantity()返回 aInteger而不是 a&nbsp;String。也许,你可以试试:TextView&nbsp;quanT&nbsp;=&nbsp;hholded.quantityItemView; quanT.setText(current.getQuantity().toString());

温温酱

尝试这样做——在onBindViewHolder中 替换——quanT.setText(current.getQuantity());和quanT.setText(current.getQuantity()+"");setText() 方法只接受字符串值,数量是整数值..所以这里可能会崩溃..

阿波罗的战车

代替quanT.setText(current.getQuantity());和quanT.setText(String.valueOf(current.getQuantity()));setText()仅适用于字符串。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java