实施回收适配器时应用程序崩溃

App crash while implementing Recycle Adapter

我正在创建一个应用程序来简单地存储牵引记录并能够查看记录。

在 IteamListActivity 中,我想查看我的牵引记录。

我想在回收视图中列出所有数据项。

当我在 activity_home xml 中单击 'View' 按钮时,应用程序崩溃。

项目的

GitHubLink是Here

代码是

适配器

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.viewHolder>{

ArrayList<DataModel> list;
Context context;

public DataAdapter(ArrayList<DataModel> list, Context context) {
    this.list = list;
    this.context = context;
}

@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = 
 LayoutInflater.from(context).inflate(R.layout.data_list_design,parent,false);
    return new viewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {

    DataModel model = list.get(position);
    holder.sn.setText(model.getSn());
    holder.date.setText(model.getDate());
    holder.name.setText(model.getName());
    holder.weight.setText(model.getWeight());
    holder.rate.setText(model.getRate());
    holder.total.setText(model.getTotal());

}

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

public class viewHolder extends  RecyclerView.ViewHolder {

TextView sn,date,name,weight,rate,total;
public viewHolder(@NonNull View itemView) {

    super(itemView);

    sn = itemView.findViewById(R.id.snTitle);
    date = itemView.findViewById(R.id.dateTitle);
    name = itemView.findViewById(R.id.nameTitle);
    weight = itemView.findViewById(R.id.weightTitle);
    rate = itemView.findViewById(R.id.rateTitle);
    total = itemView.findViewById(R.id.totalTitle);
 }
 }

 }

主页活动

        binding.listview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this,ItemListActivity.class );
                startActivity(intent);
            }
        });

   

ItemListActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityItemListBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
        ArrayList<DataModel> list = new ArrayList<>();
        list.add(new DataModel(1,"2021-07-29","Potato", 76,50,100));
        list.add(new DataModel(2,"2021-07-30","Potato", 106,60,100));
        list.add(new DataModel(3,"2021-08-02","Potato", 83,80,100));
        list.add(new DataModel(4,"2021-08-03","Potato", 67,90,100));
        list.add(new DataModel(5,"2021-08-05","Potato", 70,70,100));

        DataAdapter adapter = new DataAdapter(list,this);
        LinearLayoutManager linearLayoutManager = new 
        LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        binding.datalistRv.setLayoutManager(linearLayoutManager);
        binding.datalistRv.setNestedScrollingEnabled(false);
        binding.datalistRv.setAdapter(adapter);

    }

解决方案是

    @Override
    public void onBindViewHolder(@NonNull viewHolder holder, int position) {

        DataModel model = list.get(position);
        holder.sn.setText(model.getSn().toString());
        holder.date.setText(model.getDate());
        holder.name.setText(model.getName());
        holder.weight.setText(model.getWeight().toString());
        holder.rate.setText(model.getRate().toString());
        holder.total.setText(model.getTotal().toString());

    }

暗恋的原因是 model.getSn(), model.getWeight(), model.getRate(), model.getTotal() return 整数和 textView.setText() 调用 public final void setText(@StringRes int resid) 尝试通过您从模型中提供的整数查找字符串。