ezhil
ezhil

Reputation: 2606

How to add more items at run time in spinner

if we has a some items in the spinner means (like below) how shall we add extra items while run time

here is code:

    String[] Items = {
                "Alarm",
                "Office",
                "Meeting",
                "Party",
                "Lunch",
                "Breakfast",
                "Supper",
                "Home",
                "Private",
                "Outdoor",
                "Family",
                "Friends",
                "Others"
        };

    Spinner s1;

and

  s1 = (Spinner) findViewById(R.id.spinner);

   ArrayAdapter<String> adapter  = new ArrayAdapter<String>(
            this,android.R.layout.simple_spinner_item,Items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter);
    s1.setOnItemSelectedListener(new OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView<?> arg0, 
        View arg1, int arg2, long arg3) 
        {
            int index = s1.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), 
                "You have selected item : " + Items[index], 
                Toast.LENGTH_SHORT).show(); 

            if (index==12)
            {
                EditText edit = (EditText) findViewById(R.id.edittext);
                Button add=(Button) findViewById(R.id.add);
                edit.setVisibility(View.VISIBLE);
                add.setVisibility(View.VISIBLE);


         }
            else
            {
                EditText edit = (EditText) findViewById(R.id.edittext);
                Button add=(Button) findViewById(R.id.add);
                edit.setVisibility(View.GONE);
                add.setVisibility(View.GONE);
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {}
    });  

}

In it if select the "other" means we ve to add extra items in spinner list..

thank you,

Upvotes: 2

Views: 7098

Answers (3)

Amit Garg
Amit Garg

Reputation: 551

Following works for me:

/* Read values from resource into an array */
String[] strColorValues =  getResources().getStringArray(R.array.colors);

ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < strColorValues.length; i++) {
    list.add(strColorValues[i]);
}

ArrayAdapter adapterColors = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);

adapterColors.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinnerColors.setAdapter(adapterPermissionLevels);
spinnerColors.setOnItemSelectedListener(this);

/* Remove first element from the adapter and notify dataset changed. */
String item = spinnerColors.getItemAtPosition(0).toString();
adapterColors.remove(item);
adapterColors.notifyDataSetChanged();

Upvotes: 0

Pedro Fraca
Pedro Fraca

Reputation: 185

You can create a BaseAdapter, fill it in runtime and assign the adapter to the spinner.

Upvotes: 1

Aman Aalam
Aman Aalam

Reputation: 11251

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.

Upvotes: 3

Related Questions