Note that there are some explanatory texts on larger screens.

plurals
  1. POInflating a view multiple times in the same parent when a button is clicked
    text
    copied!<p>My app is about creating surveys. In order to do so, the user should be able to create new fields dynamically.</p> <p>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).</p> <p>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.</p> <p>I read somewhere that you cannot inflate the same child 2 times in a parent unless it is done in a loop.</p> <p>So, my questions are:</p> <p>-> How can I get around this and obtain the desired effect?</p> <p>-> Why doesn't this apply in a Loop?</p> <p>Any help or hint is appreciated. Once again, thank you very much for all the support the community has given me.</p> <p>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.</p> <p>SOurce code:</p> <p>XML - Main cointainer:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:id="@+id/wrapper" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>XML -Inflated:</p> <pre><code>&lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;TableRow&gt; &lt;TextView android:id="@+id/mdsnew_field_tv" style="@style/dsn_field" android:text="Field Name " /&gt; &lt;TextView android:id="@+id/mdsnew_type_tv" style="@style/dsn_field" android:text="Type of Field " /&gt; &lt;TextView android:id="@+id/mdsnew_choices_tv" style="@style/dsn_field" android:text="Choices" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;EditText android:id="@+id/mdsnew_fieldname_et" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Field Name" android:textSize="18sp" /&gt; &lt;Spinner android:id="@+id/mdsnew_type_sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;EditText android:id="@+id/mdsnew_choices_et" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choices " android:textSize="18sp" /&gt; &lt;/TableRow&gt; &lt;/TableLayout&gt; </code></pre> <p>Add Button:</p> <pre><code>LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper); LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.mds_new_row, myRoot); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload