Search code examples
androidandroid-adapterandroid-recyclerviewillegalstateexception

Error while duplicating a view: The specified child already has a parent


In my RecylcerAdapter, I want to duplicate a TextView.

I have a LinearLayout that contains a TexView. All I want is to dynamically duplicate this TextView inside the LinearLayout, so that I have 10 TextViews inside the LinearLayout.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tags_ll"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal"
        android:gravity="right">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tags"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="عمومی"
            android:layout_margin="5dp"
            android:padding="5dp"
            android:background="@drawable/border_with_background"
            android:textSize="12dp" />
    </LinearLayout>

MyViewHolder in RecyclerView:

public MyViewHolder(View view) {
    super(view);

    tags = (TextView) view.findViewById(R.id.tags);
    tags_ll = (LinearLayout) view.findViewById(R.id.tags_ll);

    for (int i = 0; i < 10; i++) {
        TextView rowTextView = new TextView(view.getContext());
        //Clone the new textview, get all the properties of the existing textview
        rowTextView = tags;
        rowTextView.setText("This is row #" + i);
        tags_ll.addView(rowTextView);

    }

}

I get the following error:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I want all the properties of the existing textview to be copied to the newly created TextViews.


Solution

  • What you're doing is wrong, you can't copy the TextView properties with rowTextView = tags;, you're just replacing rowTextView TextView with tags one.

    You need to set the new TextView properties from your code:

    for (int i = 0; i < 10; i++) {
        TextView rowTextView = new TextView(view.getContext());
        //set the new TextView properties from code
        rowTextView.setText("This is row #" + i);
        tags_ll.addView(rowTextView);
    
    }
    

    Or

    You can create an xml layout containing just a TextView with the desired attributes, and inflate it in your for-loop.

    eg:

    my_text_view.xml :

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:text="عمومی"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:background="@drawable/border_with_background"
        android:textSize="12dp" />
    

    In your ViewHolder :

     LayoutInflater inflater = LayoutInflater.from(context);
    
     for (int i = 0; i < 10; i++) {
    
          TextView rowTextView= (TextView) inflater.inflate(R.layout.my_text_view, null, false);
          rowTextView.setText("This is row #" + i);
          tags_ll.addView(rowTextView);
    
     }