Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add this view dynamically

I want to add some view dynamically.

LinearLayout mDescriptionLayout = (LinearLayout)findViewById(R.id.descriptionLayout); 

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate (R.layout.bonus_info_item, null,false);
TextView counter = (TextView) view.findViewById(R.id.counter);
TextView descriptionText = (TextView) view.findViewById(R.id.descriptionText);
String [] s = {"","A","B", "C","D"};

for (int i = 1; i < 5; i++){
    counter.setText(String.valueOf(i));
    descriptionText.setText(s[i]);
    mDescriptionLayout.addView(view);
}

This is my main.xml and I want add this inflated view in LinearLayout(@id/descriptionLayout)

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
                android:id="@+id/days"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"   
                android:textSize="12sp"/>

        <LinearLayout
                android:id="@+id/descriptionLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/bonusCardTitleText"
                android:orientation="vertical">

        </LinearLayout>
        </RelativeLayout>
    </ScrollView>

I don't understand what is the problem in here that I can't add view dynamically and I always got this error.

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
        at android.view.ViewGroup.addView(ViewGroup.java:3210)
        at android.view.ViewGroup.addView(ViewGroup.java:3155)
        at android.view.ViewGroup.addView(ViewGroup.java:3131)

2 Answers

As the error says,

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

you are trying to add a child View to a parent and the child already has a parent( after the first iteration of your for loop). Either declaring and initializing the View inside the for loop or calling remove() each time should fix it. I'm not sure if you are trying to add this layout multiple times like that but if so then you can try

for (int i = 1; i < 5; i++)
{
    View view = inflater.inflate (R.layout.bonus_info_item, null,false);
    TextView counter = (TextView) view.findViewById(R.id.counter);
    TextView descriptionText = (TextView) view.findViewById(R.id.descriptionText);
    counter.setText(String.valueOf(i));
    descriptionText.setText(s[i]);
    mDescriptionLayout.addView(view);
}

Another way would be to call mDescriptionLayout.removeView(view); but I don't think you want to do this in your situation. Initializing a new View with each iteration through the loop should solve your problem.

like image 169
codeMagic Avatar answered Aug 17 '26 09:08

codeMagic


You are adding the same TextView object more than once to the LinearLayout. You need to create a new instance of TextView for each item.

for (int i = 1; i < 5; i++){
    View view = inflater.inflate (R.layout.bonus_info_item, null,false);
    TextView counter = (TextView) view.findViewById(R.id.counter);
    TextView descriptionText = (TextView) view.findViewById(R.id.descriptionText);
    counter.setText(String.valueOf(i));
    descriptionText.setText(s[i]);
    mDescriptionLayout.addView(view);
}
like image 22
Karakuri Avatar answered Aug 17 '26 11:08

Karakuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!