开发者

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/AndroidRunti开发者_StackOverflow中文版me(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)


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.


You may use add method like below..

m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);  
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("dummy item");        

////////////////////////////////////////////////////////////////
addButton.setOnTouchListener(new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent event) {

        final Dialog dialog=new Dialog(roughact.this);
        dialog.setContentView(R.layout.dialog);
        dialog.setTitle("Enter The Item");
        dialog.setCanceledOnTouchOutside(true);

        final EditText filename=(EditText)dialog.findViewById(R.id.filename);
        filename.setText("");

        Button d_ok=(Button)dialog.findViewById(R.id.d_ok);
        Button d_cancel=(Button)dialog.findViewById(R.id.d_cancel);
        d_ok.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v,MotionEvent me){

                CharSequence textHolder = "" + filename.getText();
                m_adapterForSpinner.add(textHolder);

                dialog.dismiss();
                return false;
            }
        });
        d_cancel.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v,MotionEvent me){
                dialog.dismiss();
                return false;
            }
        });
        dialog.show();

        return false;
    }

Try this..It will help u.


You'll need to create a custom Adapter, that will probably be based on List<String>, and implement the add method yourself. In the add method, you'll need to add to the adapter's internal list of items (strings) The result of createFromResource is based on CharSequence[], which is returned with a fixed size and is not increased dynamically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜