ViewHolder views must not be attached when created
In my case, I fixed it changing this line
val view = parent.inflate(R.layout.your_item)
to
val view = parent.inflate(R.layout.your_item, false)
Actually, your ViewHolder
expects a View
inflated from R.layout.text_row_item
rather than a descendant of the latter. So, if you pass the inflated view the problem will be resolved.
So, you should correct your code to this:
public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = v.findViewById(r.id.display_name);
}
}
public MyRvAdapter(String[] myDataset) {
mDataset = myDataset;
}
@NonNull
@Override
public MyRvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.text_row_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(@NonNull MyRvAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}