我是 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()); }
3 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
我想错误在这里:
TextView quanT = hholded.quantityItemView; quanT.setText(current.getQuantity());
current.getQuantity()
返回 aInteger
而不是 a String
。也许,你可以试试:
TextView quanT = hholded.quantityItemView; quanT.setText(current.getQuantity().toString());
温温酱
TA贡献1752条经验 获得超4个赞
尝试这样做——在onBindViewHolder中 替换——
quanT.setText(current.getQuantity());
和
quanT.setText(current.getQuantity()+"");
setText() 方法只接受字符串值,数量是整数值..所以这里可能会崩溃..
阿波罗的战车
TA贡献1862条经验 获得超6个赞
代替
quanT.setText(current.getQuantity());
和
quanT.setText(String.valueOf(current.getQuantity()));
setText()
仅适用于字符串。
添加回答
举报
0/150
提交
取消