Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement add item in spinner array adapter in android

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)
like image 279
Narasimha Avatar asked Feb 24 '23 23:02

Narasimha


1 Answers

ArrayAdapter is already based on List, so you don't need to make a custom adapter just to use List. However, it uses the List or array it's created with. It doesn't copy the elements into a new List. If you give it a fixed-length array, then you can't add or remove members.

Create a List to pass to the constructor. Note that a List created from Arrays.asList() won't work because that is just a representation of the original array.

        spinner = (Spinner)this.findViewById(R.id.dynamicSpinner);
        CharSequence[] itemArray = 
                getResources().getTextArray(R.array.simple_from_length);
        List<CharSequence> itemList = 
                new ArrayList<CharSequence>(Arrays.asList(itemArray));

        adapter = new ArrayAdapter(this, 
                android.R.layout.simple_spinner_item,
                itemList);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);

You could also use a List<String> and getResources.getStringArray(), or whatever is convenient.

like image 149
erichamion Avatar answered Feb 27 '23 11:02

erichamion