Skip to content Skip to sidebar Skip to footer

How Do I Dynamically Update A Spinner Using Arraylist?

I have created a Spinner that is populated with an ArrayList. I want to dynamically add values to the ArrayList, so the the Spinner populates dynamically. However, when I try to ad

Solution 1:

Use the code below. This code will add a new item when the user selects and add a new item from the spinner.

Code sample:

layout main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:weightSum="10" ><Spinnerandroid:id="@+id/cmbNames"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

layout spinner_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tvName"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

Activity class:

publicclassMainActivityextendsActivity {
    privatestatic final StringNAME = "name";
    privatestatic final StringADD_NEW_ITEM = "Add New Item";

    privateSimpleAdapter adapter;
    privateSpinner cmbNames;
    privateList<HashMap<String, String>> lstNames;
    private int counter;

    privateOnItemSelectedListener itemSelectedListener = newOnItemSelectedListener() {

        @OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            HashMap<String, String> map = lstNames.get(arg2);
            String name = map.get(NAME);
            if (name.equalsIgnoreCase(ADD_NEW_ITEM)) {
                lstNames.remove(map);
                counter++;
                addNewName(String.valueOf(counter));
                addNewName(ADD_NEW_ITEM);
                adapter.notifyDataSetChanged();
            }
        }

        @OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    };

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        populateList();

        cmbNames = (Spinner) findViewById(R.id.cmbNames);
        adapter = newSimpleAdapter(this, lstNames, R.layout.spinner_item,
                newString[] { NAME }, new int[] { R.id.tvName });
        cmbNames.setAdapter(adapter);
        cmbNames.setOnItemSelectedListener(itemSelectedListener);
    }

    privatevoidpopulateList() {
        lstNames = newArrayList<HashMap<String, String>>();

        addNewName("abc");
        addNewName("pqr");
        addNewName("xyz");
        addNewName(ADD_NEW_ITEM);
    }

    privatevoidaddNewName(String name) {
        HashMap<String, String> map = newHashMap<String, String>();
        map.put(NAME, name);
        lstNames.add(map);
    }
}

Solution 2:

Try to call delete on adapterdeletetype instead of the arraylist. If it fails, then please post your logcat output.

Post a Comment for "How Do I Dynamically Update A Spinner Using Arraylist?"