Android 为什么不能从 ArrayAdapter 添加/删除项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3476723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:21:40  来源:igfitidea点击:

Why can't one add/remove items from an ArrayAdapter?

androidandroid-widgetandroid-2.2-froyo

提问by mmo

I am using an ArrayAdapter<CharSequence>to populate the items to list in a android.widget.Spinner. That works all fine.

我正在使用ArrayAdapter<CharSequence>来填充要在android.widget.Spinner. 这一切正常。

But now I want to keep the list of items dynamic, i.e. I want to be able to add/remove items from the selection list at runtime. However, when I call adapter.add(item)or adapter.remove(item)I always get a UnsupportedOperationException, even though the Javadocs of the ArrayAdapterclass describe these two methods as to be usable for exactly that intended purpose.

但现在我想保持项目列表的动态,即我希望能够在运行时从选择列表中添加/删除项目。然而,当我调用adapter.add(item)adapter.remove(item)我总是得到一个UnsupportedOperationException,即使ArrayAdapter该类的Javadocs将这两种方法描述为完全可以用于预期目的。

Is this a bug, really not implemented or what am I missing here?

这是一个错误,真的没有实现还是我在这里遗漏了什么?

回答by CommonsWare

You probably initialized the adapter with a plain Java array (e.g., String[]). Try using something that implements the java.util.Listinterface (e.g., ArrayList<String>).

您可能使用普通的 Java 数组(例如,String[])初始化了适配器。尝试使用实现java.util.List接口的东西(例如,ArrayList<String>)。

回答by zeratul021

I know it's late but just a quick explanation: it's because method Arrays.asList(T... array) returns custom inner class named ArrayListthat is read-only. As already said, you need to provide full impl. e.g. java.util.ArrayList.

我知道这已经晚了,但只是一个简单的解释:这是因为方法 Arrays.asList(T... array) 返回名为ArrayList 的自定义内部类,它是只读的。如前所述,您需要提供完整的实现。例如 java.util.ArrayList。

回答by benvd

Here's the source code of ArrayAdapter#remove:

这里的源代码ArrayAdapter#remove

public void remove(T object) {
    if (mOriginalValues != null) {
        synchronized (mLock) {
            mOriginalValues.remove(object);
        }
    } else {
        mObjects.remove(object);
    }
    if (mNotifyOnChange) notifyDataSetChanged();
}

The only thing that can throw an UnsupportedOperationExceptionthere is the line in the else-block. So the problem is that the list you're using doesn't support removing items. My guess is you're using an array. Try an ArrayList, for instance.

唯一可以抛出UnsupportedOperationExceptionthere 的是 else 块中的行。所以问题是您使用的列表不支持删除项目。我的猜测是你正在使用一个数组。例如,尝试使用 ArrayList。

edit: So yeah, what Mark said...

编辑:所以是的,马克所说的......

回答by user1063503

I was having the same problem, my data was saved in resource String Array, so I was creating ArraAdapter with createFromResource.
The following code for creating ArrayAdapter from resource String Array solved the problem:

我遇到了同样的问题,我的数据保存在资源字符串数组中,所以我用createFromResource创建了ArraAdapter
下面从资源字符串数组创建ArrayAdapter的代码解决了这个问题:

Resources res = getResources();
String[] cities = res.getStringArray(R.array.cities_array);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(
     this,
     android.R.layout.simple_spinner_item,
     new ArrayList(Arrays.asList(cities)));

回答by Tom

In your adapter Class - Delete an Item

在您的适配器类中 - 删除一个项目

remove(position);
notifyDataSetChanged();

Add an Item -

添加项目 -

adapter.add (newItem);
adapter.notifyDataSetChanged ();

回答by Prince Bhardwaj

Probably, you are using List in your ArrayAdapterclass instead of ArrayList.

可能,您在ArrayAdapter类中使用 List而不是ArrayList

Try converting your array or list to ArrayList-

尝试将您的数组或列表转换为ArrayList-

new ArrayList<ClassType>(Arrays.asList(array));