My application is creating polls. To do this, the user must be able to create new fields dynamically.
So, when creating a new poll, activity is displayed with only two buttons (add and delete). When the user clicks the add button, a line appears containing EditText (so that he can write the question), Spinner (which allows the user to select the type of field: text, RadioButton, CheckBox, etc.) And another EditText (in which various available answer options are written if applicable).
To do this, I created an XML file containing these Widgets, and each time the user clicks the add button, the XML is inflated. It works the first time I press a button, but nothing happens after that.
I read somewhere that you cannot inflate the same child 2 times in the parent unless it is done in a loop.
So my questions are:
-> How can I get around this and get the desired effect?
-> Why is this not applicable in Loop?
Any help or hint is appreciated. Thanks again for all the support provided by my community.
NOTE. In the android documentation, they talk about cloning the inflator, but there is little information there, and I could not find any other information about it.
SOURCE Code:
XML - Core 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);