1

What i want to do is to add something in a spinner by clicking on it and then click in a "add option" tha appears in the spinner. I am new to android development. Can somebody help?

Thanks

thathashd
  • 1,022
  • 4
  • 17
  • 49

2 Answers2

2

You can create an adapter (for example, an ArrayAdapter) and then bind it to your spinner using the Spinner.setAdapter() method.

After you override the Adapter.onItemSelected() method in your onItemSelectedListener, you can use a function like this (if you've implemented your onItemSelectedListener within the same class that you've declared your adapter called arrayAdapter):

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

    if (parent.getItemAtPosition(pos).toString().equals("Add Item")) {
         arrayAdapter.add(new object());  //object is what your array contains
    }
}

Look at the spinner tutorial for the in-between steps.

John Leehey
  • 22,052
  • 8
  • 61
  • 88
  • and can i populate a spinner with a array adapter and from a sqlite database on the same time? – thathashd May 14 '11 at 10:48
  • When using an sqlite database, it would probably be easier to use a cursoradapter instead. With some translation functions, you could use an array adapter too though. – John Leehey May 16 '11 at 17:13
0

For an example of dynamically adding items to a Spinner, take a look at Populate Spinner dynamically in android from edit text

Other than this, what have you got so far? Where specifically are you stuck?

Community
  • 1
  • 1
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • i can populate a spinner from an existing sqlite database. But i want to add a functionality that allows me to click on an existing option that exist inside the spinner for instance "Add a new something" and when i click in this option i am redirect to a new activity in wich i can add a new "something" to the spinner. – thathashd May 14 '11 at 10:51