从自定义列表中删除适配器项目 [英] Removing items from custom list adapter

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

问题描述

问题我有一个复选框,我想对选中的项目basis.this删除列表中的项目自定义列表视图适配器我是code

Problem i have custom list adapter view with checkboxes i want to remove list items on checked items basis.this is my code

                  for(int i=0;i<adapter.getCount();i++)
                    {   
                        System.out.println("Adapter Count:"+adapter.getCount());
                        if(checks.get(i)==true)
                        {
                            checks.put(i,false);
                            adapter.remove(feedbackList.get(i));                
                            adapter.notifyDataSetChanged();                                                 
                        }

                    }   

其基本工作正常和物品卸下,但有些项目不会得到清除即使检查......如果任何人能帮助我...
在此先感谢

its almost working fine and items are removing but some items wont get remove even checked...if any one could help me... Thanks in advance

推荐答案

您对矫正适配器,当你遍历它。你应该找一个更好的方式来做到这一点。

You are modifing your adapter, while you are iterating over it. You should find a better way to do that.

例如:

您在你的适配器3的元素和要删除所有( 1 2 3 )。

You have 3 elements in your adapter and want to remove all(1,2,3).

取出后 1 适配器只( 2 3 )离开了。

After removing 1 your adapter has only (2,3) left.

您计数 I 是您的适配器的第二场
所以,你删除 3 和你的代表就完成了。

Your counter i is on the second field of your adapter So you remove 3 and your 'for' is finished.

2 将不会被删除。

修改(一种溶液):
我没有验证它,但这应该工作:

EDIT (an solution): i didn't verify it, but this should work:

LinkedList<Integer> list = new LinkedList<Integer>();
for(int i=0;i<adapter.getCount();i++)
{   
     System.out.println("Adapter Count:"+adapter.getCount());
     if(checks.get(i)==true)
     {
         list.addFirst(i);                                           
     }
}  
//list will now contain all positions of checked items (e.g. 1,5,7,8,9,..)
for(int i=0;i<list.length;i++)
{
   //list.get(i) gets the index of the checked item   
   adapter.remove(feedbackList.get(list.get(i)));             
   adapter.notifyDataSetChanged();           
}

这篇关于从自定义列表中删除适配器项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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