I'm creating an app to simply store the records of a tractions and able to see the record.
In IteamListActivity I want to see my records of tractions.
I want to list all the data item in recycle view.
When I click 'View' button in activity_home xml, app crashes.
GitHub Link of the project is Here
Code is
Adapter
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);
}
}
}
HomeActivity
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);
}
CodePudding user response:
Solution is
@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());
}
Reason of crush is model.getSn(), model.getWeight(), model.getRate(), model.getTotal() return Integer and textView.setText() calls public final void setText(@StringRes int resid)
which tries to find string by Integers which you provided from your model.