How to Add Items to a Spinner in Android

how to add item in Spinner Android

You can use simple default ArrayAdapter to achieve your requirement dynamically. Below is the code snippet you can follow and modify your onCreate Method to add values in the spinner.

    spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter;
List<String> list;

list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

if you want the spinner values to be added statically you can add the array in the xml file and assign the attribute entries for the spinner. below is the code snippet for that.

Your layout xml file

<Spinner android:id="@+id/spinner"  
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:prompt="@string/spin"
android:entries="@array/items" />

In your arrays.xml file

<string-array name="items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
</string-array>

how to add items to the spinner dynamically at first index in android?

    ddlCarType= (Spinner) findViewById(R.id.ddlCarType);
String arr[] = getResources().getStringArray(R.array.arrCarType);
String arrN[] = new String[arr.length + 1];
arrN[0] = "All";

for (int i = 0; i < arr.length; i++) {
arrN[i + 1] = arr[i];
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx,
android.R.layout.simple_spinner_item, arrN);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ddlCarType.setAdapter(adapter);

Android Spinner add items

You are adding x to an array element then passing this element to the adapter.add method. This is effectively the same as using: adapter.add(x);.

Why not use an ArrayList instead of a array. ArrayLists are dynamic, and do not need to be given an initial size.
A very quick check has shown me that there is a constructor for an Array Adapter that is compatible with ArrayLists and I expect any descendant of the List class. See the code example below.

ArrayList<Integer> test = new ArrayList<Integer>();

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);

I know this is not a complete solution to your problem, but just a pointer to a possibly better way of doing it.

Do a web search with the terms ‘android updating arrayAdapter at runtime’ and I am sure you will stumble upon a more concrete example.

How to add a single item to an existing spinner?

For that Make List<> Variable,ArrayAdapter Globally above onCreate Method.

add defaults items in oncreate Method and configure Arrayadapter for that spinner.



Then in onActivityResult add that new value in List.add("String").



After that add this line.

adapter.notifydatasetchanges();
it will update your data into spinner control.




If need more help i will give attach example here.

How to add more items at run time in spinner

I hope you can detect the case when the user has selected "others"

in that case, add the other values in your Items array and call the function adapter.notifyDataSetChanged() it should accommodate the changes and you should see the new values in the spinner.

try this out and revert.



Related Topics



Leave a reply



Submit