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

时间:2011-02-27 13:33:33

标签: 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)

3 个答案:

答案 0 :(得分:7)

ArrayAdapter已基于List,因此您无需仅使用List来创建自定义适配器。但是,它使用它创建的List或数组。它不会将元素复制到新列表中。如果给它一个固定长度的数组,那么就不能添加或删除成员。

创建一个List以传递给构造函数。请注意,从Arrays.asList()创建的List不起作用,因为它只是原始数组的表示。

        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);

你也可以使用List&lt; String&gt;和getResources.getStringArray(),或任何方便的东西。

答案 1 :(得分:1)

您可以使用下面的添加方法..

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;
    }

试试这个..它会帮助你。

答案 2 :(得分:0)

您需要创建一个可能基于List<String>的自定义适配器,并自行实施add方法。在add方法中,您需要添加到适配器的内部项目列表(字符串) createFromResource的结果基于CharSequence[],它以固定大小返回,并且不会动态增加。