how to add item to Spinner's ArrayAdapter?

前端 未结 4 1232
渐次进展
渐次进展 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 planets = new ArrayList();
    adapter = new ArrayAdapter(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.

提交回复
热议问题