Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add item to Spinner's ArrayAdapter?

i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code:

public class Spr extends Activity {
Button bt1;
EditText et;
ArrayAdapter<CharSequence> adapter;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bt1 = (Button)this.findViewById(R.id.bt1);
    et = (EditText)this.findViewById(R.id.et);  
    spinner = (Spinner)this.findViewById(R.id.spr);

    adapter = ArrayAdapter.createFromResource(
            this, R.array.planets_array, android.R.layout.simple_spinner_item);

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

    spinner.setAdapter(adapter);

    bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String temp = et.getText().toString();

            adapter.add(temp);
            adapter.notifyDataSetChanged();
            spinner.setAdapter(adapter);

        }
    });


    spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {

            Toast.makeText(parent.getContext(), "The planet is " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }});
}

}

thanks! ...still waitting

like image 318
allen Avatar asked Mar 24 '10 03:03

allen


People also ask

How do I add items to ArrayAdapter?

Create a global ArrayList and add the contents to it using add() and pass it to ArrayAdapter. It's better to pass the List or String[] to ArrayAdapter and set that adapter to List You should update the List or Array being passed to Adapter, not Adapter itself.

How do you add entries to a spinner?

For adding item in Spinner, you can do one thing, try to create an adapter and then add/remove items into the adapter, then you can easily bind that adapter to spinner by using setAdapter() method.


2 Answers

When you have created your ArrayAdapter you haven't assigned a resizeable List to it, so when you do add() it cannot increment the size of it and throws a UnsupportedOperationException.

Try something like this:

List<CharSequence> planets = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(context,
                       R.array.planets_array, planets);
//now you can call adapter.add()

You should use a List. With an Array such as CharSequence[] you would get the same UnsupportedOperationException exception.

like image 149
Javi Avatar answered Oct 09 '22 11:10

Javi


Javi is right except don't reference an array for the second parameter.

adapter = new ArrayAdapter<CharSequence>(this,
  android.R.layout.simple_spinner_item,
  someList);
like image 3
Anthony Graglia Avatar answered Oct 09 '22 12:10

Anthony Graglia