Skip to content Skip to sidebar Skip to footer

Viewholder Views Must Not Be Attached When Created

I'm trying to create a simple RV that will show a TextView. This is my adapter: public class MyRvAdapter extends RecyclerView.Adapter { private Strin

Solution 1:

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:

publicclassMyRvAdapterextendsRecyclerView.Adapter<MyRvAdapter.ViewHolder> {

    private String[] mDataset;

    publicstaticclassViewHolderextendsRecyclerView.ViewHolder {
        // each data item is just a string in this casepublic TextView mTextView;
        publicViewHolder(View v) {
            super(v);
            mTextView = v.findViewById(r.id.display_name);
        }
    }

    publicMyRvAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @NonNull@Overridepublic MyRvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Viewv= LayoutInflater.from(parent.getContext())
            .inflate(R.layout.text_row_item, parent, false);
        ViewHoldervh=newViewHolder(v);
        return vh;
    }

    @OverridepublicvoidonBindViewHolder(@NonNull MyRvAdapter.ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }

    @OverridepublicintgetItemCount() {
        return mDataset.length;
    }
}

Solution 2:

Wait till view is created before binding.

Remove TextView userNameInList= v.findViewById(R.id.display_name); from your MyRvAdapter.ViewHolder.

And bind in ViewHolder as mTextView = v.findViewById(r.id.display_name);.

Solution 3:

In my case, I fixed it changing this line

valview= parent.inflate(R.layout.your_item)

to

valview= parent.inflate(R.layout.your_item, false)

Solution 4:

I know its too late but i want to add my answer, As it can help anybody who is facing this issue not due to above reason. In My Case i was using "Data Binding Library" and my mistake was that my root element was not direct child of <layout>

<layoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><data><variablename="viewmodel"type="com.myapp.data.ViewModel" /></data><!-- UI layout's root element --><ConstraintLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><!-- By Mistake i was Making this element as Root --></FrameLayout></ConstraintLayout></layout>

Solution 5:

try this

publicclassMyRvAdapterextendsRecyclerView.Adapter<MyRvAdapter.MyViewHolder> {

private Context mContext;
private String[] mDataset;

publicclassMyViewHolderextendsRecyclerView.ViewHolder {
    TextView title;

    MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.display_name);
    }
}

publicMyRvAdapter(Context mContext, String[] myDataset) {
    this.mContext = mContext;
    this.mDataset = myDataset;
}


@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewitemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.text_row_item, parent, false);
    returnnewMyViewHolder(itemView);
}

@OverridepublicvoidonBindViewHolder(final MyViewHolder holder, finalint position) {
    holder.title.setText(mDataset[position]);
}

@OverridepublicintgetItemCount() {
    return mDataset.length;
}
}

Post a Comment for "Viewholder Views Must Not Be Attached When Created"