1

On the start of the program,the Spinner is created by

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinnerAdapter = ArrayAdapter.createFromResource(
            this, R.array.IDs, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

at a later Point, all entrys should be replaced by Items out of an ArrayList. When using the Spinner, only the String is interesting to me. On showing and later in the algorithm.

How would you do that?

for (String object:out){
    System.out.println("added to spinner: "+object);
    spinnerAdapter.add(object);
    }


    spinnerAdapter.notifyDataSetChanged();

That was my idea, but it seems much too simple as it only throws exceptions.

Edit:

As asked, here the exception thrown..

12-11 15:11:38.538: I/System.out(280): added to spinner: TheSwitch 12-11 15:11:38.548: W/dalvikvm(280): threadid=7: thread exiting with uncaught exception (group=0x4001d800) 12-11 15:11:38.568: E/AndroidRuntime(280): FATAL EXCEPTION: Thread-8 12-11 15:11:38.568: E/AndroidRuntime(280): java.lang.UnsupportedOperationException 12-11 15:11:38.568: E/AndroidRuntime(280):    at java.util.AbstractList.add(AbstractList.java:411) 12-11 15:11:38.568: E/AndroidRuntime(280):     at java.util.AbstractList.add(AbstractList.java:432) 12-11 15:11:38.568: E/AndroidRuntime(280):     at android.widget.ArrayAdapter.add(ArrayAdapter.java:178) 12-11 15:11:38.568: E/AndroidRuntime(280):    at de.enocean.EnOceanAppActivity.updateSpinner(EnOceanAppActivity.java:109) 12-11 15:11:38.568: E/AndroidRuntime(280):  at de.enocean.EnOceanAppActivity$1.useNotifyMessage(EnOceanAppActivity.java:222) 12-11 15:11:38.568: E/AndroidRuntime(280):     at de.enocean.EnOceanAppActivity$1.run(EnOceanAppActivity.java:179) 12-11 15:11:38.568: E/AndroidRuntime(280):  at java.lang.Thread.run(Thread.java:1096)
6
  • I edited the post with the exception Commented Dec 11, 2011 at 15:14
  • Heh, looks like the Adapter created from an XML resource doesn't allow modifying its contents. I didn't know that. Perhaps you could try then to extend ArrayAdapter and add the initial values in your constructor instead of taking them from XML Commented Dec 11, 2011 at 16:15
  • I dont care, if i had to modify the initialisation. But: how would I do that? I just found examples with XML Commented Dec 11, 2011 at 16:20
  • so, if i initiate it with an array and later create a new adapter and set it in a buttonListener, 12-11 16:45:22.062: E/AndroidRuntime(329): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. Commented Dec 11, 2011 at 16:51
  • 1
    That's another matter. You have to do all the UI operations within the UI thread only. If you're doing something with UI from some other thread you should use runOnUiThread(new Runnable() { ... } } Commented Dec 11, 2011 at 18:23

1 Answer 1

3

It looks like adapters created from resources are immutable. You could, probably, populate your adapter in code like this

String[] myStrings = {"One", "Two", "Three" };
ArrayAdapter<String> adapter = new ArrayAdapter(YourActivity.this, android.R.simple_spinner_adapter, myStrings);
spinner.setAdapter(adapter);

I think the adapter created this way will allow adding/deleting of values

Sign up to request clarification or add additional context in comments.

2 Comments

answer was given in the comments above. I am accepting this here as the answer to accept an answer as both came from Alex.
But how to update thespinner tittle (current selected option)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.