简体   繁体   English

从listView android中删除项目

[英]remove items from listView android

I have a ListView that contains list of events, when i scroll the list i load more items and always i souldn't have more than 20 events visible in my screen. 我有一个包含事件列表的ListView,当我滚动列表时,我加载了更多项目,并且始终无法在屏幕上看到20个以上的事件。 So i proceed like this: 所以我继续这样:

    //load data from server and add it to my eventsList
    int size = eventsList.size();
    List<Data> temp;
    if(size>20) {
        eventsList = eventsList.subList(size - 20, size);
    }

    Log.e("eventsList Load More", " " + eventsList.size() + " " + principalSchedule.getCount());

    //Tell to the adapter that changes have been made, this will cause the list to refresh
    principalSchedule.notifyDataSetChanged();

but the problem i always have eventsList.size() = 20 and principalSchedule.getCount() does't decrease. 但问题是我总是有eventsList.size()= 20并且principalSchedule.getCount()不会减少。

List<E>#subList(int, int) method returns a new List<E> with the subset of the initial List and doesn't change the original List. List <E> #subList(int,int)方法返回具有初始List的子集的新List <E>,并且不会更改原始List。

If you aren't setting eventList back to the principalSchedule, the List from prin 如果您未将eventList设置回principalSchedule,则prin中的List

Your ListView should be interacting with your list via a ViewAdapter (probably an ArrayAdapter but your haven't shown that code) If you create your own Adapter (not very hard to do) then the getView() method on that adapter will be able to determine when an item is no longer visible and possibly reclaim the resources. 您的ListView应该通过ViewAdapter(可能是ArrayAdapter,但尚未显示该代码)与列表进行交互如果您创建自己的Adapter(不是很难做到),则该适配器上的getView()方法将能够确定何时不再可见某项,并可能回收资源。

This link: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ goes into greater detail about one technique for an adapter that loads resources in a background thread and can free the resources when they are no longer needed. 该链接: http : //lucasr.org/2012/04/05/performance-tips-for-androids-listview/详细介绍了一种适配器的技术,该适配器可在后台线程中加载资源并在以下情况下释放资源他们不再需要。 It doesn't exactly match your use case but if you read it and understand it you should see how to write the code you need for your case. 它与您的用例不完全匹配,但是如果您阅读并理解它,则应该了解如何编写针对您的用例的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM