如何删除从Android的ListActivity一个适配器项目 [英] How to remove items from a an adapter in Android ListActivity

查看:485
本文介绍了如何删除从Android的ListActivity一个适配器项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,需要在我的listActivity动态删除从足球比赛名单的球员。该活动已链接到字符串描述每个球员的数组的适配器。然后,我尝试从列表中单击时删除的球员。我的问题是,每当我使用内置的方法适配器删除我不断收到此错误。

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.

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

据我所知,我没有从适配器删除项目的权利,但我不知道为什么,因为每一个例子,我发现网上似乎没有问题的工作。这是code我用它来尝试从适配器中删除单个项目。

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
}

这是在code,我试图改变的数据源适配器和变化的通知它,希望它会更新屏幕上的列表中。

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

}

如果任何人都可以照到什么可以去上这将是更AP preciated一些轻。

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

推荐答案

如果你使用一个列表来存储你的对象,并通过适配器的ListVeiw显示出来,你要使用:

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

    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
}

这正常工作

这篇关于如何删除从Android的ListActivity一个适配器项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆