Raja Babar
Raja Babar

Reputation: 53

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<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

Upvotes: 3

Views: 6958

Answers (5)

technocrat
technocrat

Reputation: 3695

You may need to update your observers

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

Upvotes: 0

Sprigg
Sprigg

Reputation: 3319

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

Upvotes: 1

Pramod
Pramod

Reputation: 172

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

Upvotes: 1

Vivek Kumar Srivastava
Vivek Kumar Srivastava

Reputation: 2158

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

Upvotes: 0

jeet
jeet

Reputation: 29199

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

                    }

Upvotes: 0

Related Questions