Android 如何将项目添加到 Spinner 的 ArrayAdapter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2505207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add item to Spinner's ArrayAdapter?
提问by allen
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:
我有一个 EditText 、一个按钮和一个微调器。单击按钮 时,微调器将添加一个新项目,名称为您在 EditText 中输入的名称。但问题是,我的adapter.add() 方法似乎不起作用……这是我的代码:
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
谢谢!……还在等
回答by Javi
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.
当您创建 ArrayAdapter 时,您尚未为其分配可调整大小的列表,因此当您执行 add() 时,它无法增加它的大小并抛出 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.
您应该使用列表。使用诸如 CharSequence[] 之类的数组,您将获得相同的 UnsupportedOperationException 异常。
回答by trgraglia
Javi is right except don't reference an array for the second parameter.
Javi 是对的,只是不要为第二个参数引用数组。
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,
someList);
回答by AdamC
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.
我相信这是按设计工作的,但不像预期的那样工作。ArrayAdapter 过去只接受一个数组,但后来添加了列表构造函数。我猜它只是在你的列表上做一个 toArray() 。这就是为什么您必须在适配器上调用 add 或在列表更改时创建新适配器的原因。
回答by Hossein
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
您可以创建一个arraylist并将所有资源复制到该对象然后创建arrayadaptor并发送这个arraylist并在按钮的onclicklistener中,将edittext内容添加到arraylist对象然后调用adator的notifydatasetchanged