How do I repeat an existing xml spinner dynamically

FoErfrojeriom

New Member
I have an XML file: my_form.xml (abridged to just show relevant views). The spinner is populated by a string array from strings.xml\[code\]<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:orientation="vertical" ><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Spinner android:id="@+id/task" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/task_array" android:prompt="@string/task" /> <TextView android:id="@+id/addTask" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add another task" android:onClick="onClick" android:clickable="true"/></LinearLayout></ScrollView> \[/code\]So I have an activity that displays the xml spinner and text view. When the text view is clicked I want to add another instance of the same xml spinner and text view, so that a person can add as many tasks as they like. I dont want a multi-select listView as I am going to have to associate each chosen item/task with other individual methodsCan anyone help me please?\[code\]public class MyFormActivity extends FragmentActivity implements OnClickListener { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_form); final Spinner spinnerTask = (Spinner) findViewById(R.id.task); ArrayAdapter<CharSequence> adapter_task = ArrayAdapter .createFromResource(this, R.array.task_array, android.R.layout.simple_spinner_item); spinnerTask.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) { int taskChoice = spinnerTask.getSelectedItemPosition(); SharedPreferences sharedPref = getSharedPreferences("FileName", 0); SharedPreferences.Editor prefEditor = sharedPref.edit(); prefEditor.putInt("taskChoiceSpinner", taskChoice); prefEditor.commit(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } View tvAddTask = (TextView) findViewById(R.id.addTask); tvAddTask.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //Here I want add another instance of the spinner and text view above } }\[/code\]
 
Back
Top