Skip to content Skip to sidebar Skip to footer

Removing Items From Custom List Adapter

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

Solution 1:

ArrayAdapter<String> adptr= newArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text,list)
Stringdelete =adptr.getItem(0));
adptr.remove(delete);

Solution 2:

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

Example:

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

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

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

2 will not be removed.

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();           
}

Solution 3:

remove array item from the position and reload list by notifyDataSetChanged, method:

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

                    }

Solution 4:

Try below code

for(int i=0;i<feedbackList.size();i++)
        {
            if(checks.get(i) == true)
            {
                feedbackList.remove(i);
                checks.remove(i);
                i--;
            }
        }


adapter.notifyDataSetChanged();  

and use feedbackList and checks as a Class level variables

Solution 5:

You may need to update your observers

for(DataSetObserver observer : observers) {
        observer.onChanged();
    }

Post a Comment for "Removing Items From Custom List Adapter"