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

时间:2021-06-29 21:04:17

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

我正在使用ArrayAdapter 来填充要在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 ArrayAdapter class 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?

这是一个错误,真的没有实现或我在这里缺少什么?

6 个解决方案

#1


118  

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

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

#2


20  

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

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

#3


16  

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 UnsupportedOperationException there 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.

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

edit: So yeah, what Mark said...

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

#4


12  

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。以下用于从资源String Array创建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)));

#5


3  

In your adapter Class - Delete an Item

在适配器类中 - 删除项

remove(position);
notifyDataSetChanged();

Add an Item -

添加项目 -

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

#6


0  

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

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

Try converting your array or list to ArrayList -

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

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

#1


118  

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

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

#2


20  

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

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

#3


16  

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 UnsupportedOperationException there 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.

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

edit: So yeah, what Mark said...

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

#4


12  

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。以下用于从资源String Array创建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)));

#5


3  

In your adapter Class - Delete an Item

在适配器类中 - 删除项

remove(position);
notifyDataSetChanged();

Add an Item -

添加项目 -

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

#6


0  

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

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

Try converting your array or list to ArrayList -

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

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