Inflating a view multiple times in the same parent when a button is clicked
Asked Answered
I

1

5

My app is about creating surveys. In order to do so, the user should be able to create new fields dynamically.

So, when creating a new survey, an Activity is shown with only 2 buttons (add and delete). When a user clicks the add button, a row appears containing a EditText(so he can write the question), a Spinner(that enables user to select the type of field: text, RadioButton, CheckBox, etc..) and another EditText (in which the different available answer choices are written, if applicable).

To accomplish this I created a XML file containing these Widgets and each time the user clicks in the add button, the XML is inflated. It works the first time I press the button, but after that... nothing happens.

I read somewhere that you cannot inflate the same child 2 times in a parent unless it is done in a loop.

So, my questions are:

-> How can I get around this and obtain the desired effect?

-> Why doesn't this apply in a Loop?

Any help or hint is appreciated. Once again, thank you very much for all the support the community has given me.

NOTE: In the android documentation they speak about cloning an inflator but the information there is scarce and I couldn't find any other info about it.

SOurce code:

XML - Main cointainer:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <Button
        android:id="@+id/mds_new_addfield_button"
        android:tag="new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Field"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
    />
    <Button
        android:id="@+id/mds_new_deletefield_button"
        android:tag="delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete Field"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
    />

</RelativeLayout>

XML -Inflated:

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
    <TableRow>
        <TextView
            android:id="@+id/mdsnew_field_tv"
            style="@style/dsn_field"
            android:text="Field Name    " 
        />

        <TextView
            android:id="@+id/mdsnew_type_tv"
            style="@style/dsn_field"
            android:text="Type of Field   " 
        />

        <TextView
            android:id="@+id/mdsnew_choices_tv"
            style="@style/dsn_field"
            android:text="Choices"  
        />
    </TableRow>

    <TableRow>

        <EditText
            android:id="@+id/mdsnew_fieldname_et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Field Name"
            android:textSize="18sp"
        />

        <Spinner
            android:id="@+id/mdsnew_type_sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

        <EditText
            android:id="@+id/mdsnew_choices_et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Choices    "
            android:textSize="18sp"
        />

    </TableRow>
</TableLayout>

Add Button:

LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper);

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.mds_new_row, myRoot);
Island answered 29/11, 2010 at 0:11 Comment(3)
You can inflate the same child two, or more times. What you can't do is to use the same instance of a view more than once. Each time you call the inflate method of LayoutInflater you are creating a new View so you shouldn't have problems with the way you are doing things. So... may be the error is somewhere else... could you please share the XML files (the container and the one that is inflated in runtime)? Also, the snippet of code where you inflate your View will be helpful.Maximilianus
Edited the post to include what you asked! (thanks for your help!)Island
How will you retrieve the id for each view.Collings
M
13

Your parent is a LinearLayout, and it looks like you intended a vertical orientation, but you haven't specified this. Try setting the orientation and see if that helps.

Mcnamee answered 29/11, 2010 at 1:56 Comment(4)
Yup! That was the problem. Thanks a million.Island
Thanks @Mcnamee i googled lots and wasted 2 days then i find this solutions.Pavior
I can't believe it took me this long to solve this issue... I assumed that vertical orientation was implied as it is when defining elements in XML! Thank you.Shaum
Man! Life saver! :*Lammastide

© 2022 - 2024 — McMap. All rights reserved.