天天看点

android spinner自定义适配器,如何在android中的spinner数组适配器中实现添加项目

我有一个EditText,一个按钮和一个微调器.单击该按钮时,微调器将添加一个新项目,其中包含您在EditText中输入的名称.但这是问题,我的adapter.add()方法似乎不起作用…这是我的代码:

bt1 = (Button)this.findViewById(R.id.AddBtn);

et = (EditText)this.findViewById(R.id.newSpinnerItemText);

spinner = (Spinner)this.findViewById(R.id.dynamicSpinner);

adapter = ArrayAdapter.createFromResource(

this, R.array.simple_from_length, 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);

}

});

这个文件的错误:

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): java.lang.UnsupportedOperationException

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at java.util.AbstractList.add(AbstractList.java:410)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at java.util.AbstractList.add(AbstractList.java:432)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at org.example.SpinnerKiran.SpinnerKiran$1.onClick(SpinnerKiran.java:56)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.View.performClick(View.java:2179)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.View.onTouchEvent(View.java:3828)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.widget.TextView.onTouchEvent(TextView.java:6291)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.View.dispatchTouchEvent(View.java:3368)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.app.Activity.dispatchTouchEvent(Activity.java:1993)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.os.Handler.dispatchMessage(Handler.java:99)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.os.Looper.loop(Looper.java:123)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at android.app.ActivityThread.main(ActivityThread.java:3948)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at java.lang.reflect.Method.invokeNative(Native Method)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at java.lang.reflect.Method.invoke(Method.java:521)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)

02-27 18:01:17.728: ERROR/AndroidRuntime(1982): at dalvik.system.NativeStart.main(Native Method)