Java 如何从 Android ListActivity 中的适配器中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18837747/
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-12 11:31:30  来源:igfitidea点击:

How to remove items from an adapter in Android ListActivity

javaandroidlistactivity

提问by cmw2379

I am writing an android app that needs to dynamically remove players from a soccer game roster in my listActivity. The Activity has an adapter that links to an array of Strings describing each player. I then try to remove a player from the list when clicked. My problem is that whenever I use the adapters built in methods to remove I keep getting this error.

我正在编写一个 android 应用程序,它需要从我的 listActivity 中的足球比赛名单中动态删除球员。Activity 有一个适配器,该适配器链接到描述每个玩家的字符串数组。然后我尝试在单击时从列表中删除播放器。我的问题是,每当我使用内置方法删除适配器时,我都会收到此错误。

 FATAL EXCEPTION: main
 java.lang.UnsupportedOperationException
 at java.util.AbstractList.remove(AbstractList.java:638);

As far as I can tell, I don't have the right to remove items from the adapter, but I don't get why since every example I have found online seems to work with no problems. This is the code I use to try and remove a single item from the adapter.

据我所知,我无权从适配器中删除项目,但我不明白为什么,因为我在网上找到的每个示例似乎都没有问题。这是我用来尝试从适配器中删除单个项目的代码。

    public void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    adapter.remove(adapter.getItem(position));
    adapter.notifyDataSetChanged(); //Updates adapter to new changes
}

This is the code where I tried to change the data source for the adapter and notify it of changes in hopes it would update the onscreen list.

这是我尝试更改适配器数据源并通知它更改的代码,希望它能更新屏幕列表。

    public void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    mainRoster.removePlayer(position); //removes from custom roster
    stringedRoster = mainRoster.getStringArray();  //Resets string array
    adapter.notifyDataSetChanged(); //Updates adapter to new changes

}

If anybody can shine some light on what could be going on it would be much appreciated.

如果有人可以对可能发生的事情有所了解,我们将不胜感激。

采纳答案by marshallino16

if you're using a List to store your Object and display them in a ListVeiw through the adapter, you've to use :

如果您使用 List 来存储对象并通过适配器将它们显示在 ListVeiw 中,则必须使用:

    public void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    list.remove(position);
    adapter.notifyDataSetChanged(); //Updates adapter to new changes
}

This works fine

这工作正常

回答by wojtek.kalicinski

You haven't actually said what class your Adapter is, which might be helpful. But if it's an android.widget.ArrayAdapter then it has two kinds of constructors - using an array of objects or a list of objects.

您实际上还没有说您的 Adapter 是什么类,这可能会有所帮助。但是如果它是一个 android.widget.ArrayAdapter 那么它有两种构造函数 - 使用对象数组或对象列表。

Now, you will only be allowed to remove objects from the adapter if the underlying list supports removal. Arrays in Java, as you probably know, are of fixed length (you cannot add or delete an element, only change its value). If I remember correctly the ArrayAdapter internally wraps the array you supply in an unmutable list.

现在,如果底层列表支持删除,您将只被允许从适配器中删除对象。您可能知道,Java 中的数组是固定长度的(您不能添加或删除元素,只能更改其值)。如果我没记错的话,ArrayAdapter 在内部将您提供的数组包装在一个不可变列表中。

Solution: supply a List instead of an Array to the ArrayAdapter constructor and MAKE SURE that list is mutable (you can use an ArrayList for example or some other implementation that supports .remove() ).

解决方案:向 ArrayAdapter 构造函数提供 List 而不是 Array 并确保该列表是可变的(例如,您可以使用 ArrayList 或支持 .remove() 的其他一些实现)。

If it's another adapter subclass you're using, then it's difficult to discuss its implementation details and why it won't remove elements if we don't know what it is and how it works.

如果它是您正在使用的另一个适配器子类,那么很难讨论它的实现细节以及如果我们不知道它是什么以及它是如何工作的,为什么它不会删除元素。