how to add item to Spinner's ArrayAdapter?

我的梦境 提交于 2019-12-18 12:19:09

问题


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


回答1:


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.




回答2:


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);



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/2505207/how-to-add-item-to-spinners-arrayadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!