how to implement add item in spinner array adapter in android
Posted
by
narasimha
on Stack Overflow
See other posts from Stack Overflow
or by narasimha
Published on 2011-02-27T13:33:32Z
Indexed on
2011/02/28
7:24 UTC
Read the original article
Hit count: 319
hi folks,
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:
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);
}
});
error of this file:
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)
© Stack Overflow or respective owner