Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to remove an item from a listView and arrayAdapter

I have a collection of items in an ArrayList. I add them to a customer adapter as follows:

this.m_adapter = new MyAdapter(this, R.layout.myitem,
    itemCart.m_items);

I have a delete button for each of these items in my list, but I am not sure how to connect the delete button's onClick() with the original item in the ArrayList. Can someone please explain how to do this or point me to a tutorial where I can read up on this? Non-sarcastic/non-condescending responses are greatly appreciated.

like image 356
gonzobrains Avatar asked Jan 15 '11 06:01

gonzobrains


People also ask

How do I remove items from list view?

To remove items programmaticallyUse the RemoveAt or Clear method of the Items property. The RemoveAt method removes a single item; the Clear method removes all items from the list.

What is the use of ArrayAdapter in Android?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


1 Answers

You can call the remove() method on your ArrayList

itemCart.m_items.remove(<index of element to remove>);
this.m_adapter.notifyDataSetChanged();

And then you need to call notifyDataSetChanged(); on your adapter to update the ListView

like image 189
jcuenod Avatar answered Sep 23 '22 06:09

jcuenod