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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…