我有一个微调器,其中的少量值来自strings.xml,我想使用Button的onclick事件从编辑文本中动态填充该微调器,但不知何故它不起作用,我每次都得到强制关闭.任何帮助将不胜感激.
package com.example.expense; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; public class Expense3 extends Activity { //private TextView t1; private Spinner spinner; private EditText Text; private ArrayAdapter<CharSequence> adapter; private Button addButton; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Text = (EditText)findViewById(R.id.widget4); addButton = (Button)findViewById(R.id.add_new); spinner = (Spinner) findViewById(R.id.hhj); 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); this.addButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { addNewSpinnerItem(); } }); } protected void addNewSpinnerItem() { // TODO Auto-generated method stub CharSequence textHolder = "" + Text.getText().toString(); adapter.add(textHolder); } }
Logcat错误: –
05-14 01:07:19.934: ERROR/AndroidRuntime(802): FATAL EXCEPTION: main 05-14 01:07:19.934: ERROR/AndroidRuntime(802): java.lang.UnsupportedOperationException 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.util.AbstractList.add(AbstractList.java:411) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.util.AbstractList.add(AbstractList.java:432) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.widget.ArrayAdapter.add(ArrayAdapter.java:178) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.example.expense.Expense3.addNewSpinnerItem(Expense3.java:50) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.example.expense.Expense3$1.onClick(Expense3.java:40) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.view.View.performClick(View.java:2408) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.view.View$PerformClick.run(View.java:8816) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.os.Handler.handleCallback(Handler.java:587) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:92) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:123) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:521) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-14 01:07:19.934: ERROR/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
解决方法
您正在尝试在从资源创建的适配器中添加项目,因此它将为您提供不受支持的结果.要解决此问题,您可以创建一个空白列表,然后从资源中添加列表中的项目.为此你可以使用Arrays.asList()或者你可以创建一个循环并在List中添加单个项目,在创建List之后你必须使用该List创建一个适配器.
现在要在微调器的适配器中添加项,可以使用adapter.add()方法在微调器适配器中添加项.
下面是一个示例,说明如何创建适配器并在微调器中添加项目.
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,array);
这里的数组是List.
adapter.add("String to insert into spinner");