how to add item to Spinner's ArrayAdapter?

前端 未结 4 1233
渐次进展
渐次进展 2020-12-30 05:21

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 adap

相关标签:
4条回答
  • 2020-12-30 05:57

    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.

    0 讨论(0)
  • 2020-12-30 06:00

    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);
    
    0 讨论(0)
  • 2020-12-30 06:17

    you can create an arraylist and copy all recourse to this object then create arrayadaptor and send this arraylist and in onclicklistener of button, add edittext content to arraylist object then call notifydatasetchanged of adator

    0 讨论(0)
  • 2020-12-30 06:21

    I believe this is working as designed, but not as expected. ArrayAdapter used to only take an array, but the list constructor was added later. I'm guessing its just doing a toArray() on your list. This is why you have to either call add on the adapter, or create a new adapter when your List changes.

    0 讨论(0)
提交回复
热议问题