i created a class for temp data to show. Obviously my app is crashing but if i change int sno to String sno then there is no problem. i want to know for int, if yes then how?
public class Contact
{
public int sno;
public String phone;
public String name;
Contact(int sno, String phone, String name)
{
this.sno = sno;
this.phone = phone;
this.name = name;
}
}
My custom adapter class file -->
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>
{
private final Contact[] localDataSet;
// private TextView textView;
private TextView textView2;
private TextView textView3;
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView textView2;
public ViewHolder(View view) {
super(view);
// Define click listener for the ViewHolder's View
textView2 = (TextView) view.findViewById(R.id.textView2);
}
public TextView getTextView() {
return textView2;
}
}
/**
* step 1 Initialize the dataset of the Adapter.
*
*/
public CustomAdapter(Contact[] dataSet) {
localDataSet = dataSet;
}
// Create new views (invoked by the layout manager)
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view, which defines the UI of the list item
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.contact_layout, viewGroup, false);
// textView = view.findViewById(R.id.textView);
textView2 = view.findViewById(R.id.textView2);
textView3 = view.findViewById(R.id.textView3);
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position)
{
// Get element from your dataset at this position and replace the
// contents of the view with that element
// textView.setText(localDataSet[position].sno);
textView2.setText(localDataSet[position].name);
textView3.setText(localDataSet[position].phone);
}
i commented out one textview temporarily to not get sno print then my app works
CodePudding user response:
If you use an int
as an argument with TextView::setText
, then you're expected to pass a resource id. Your crash, if you see the error log, will likely say something like "could not resolve resource id for [some_number]".
Convert the int
to a String
before setting it to your TextView
and you'll no longer have a problem, i.e. textView.setText(String.valueOf(localDataSet[position].sno));
CodePudding user response:
When you set int value to textView, app crashed. You can use it like this.
textView.setText(localDataSet[position].sno "")