I want to have a Button that when clicked, removes all the checked items in the ListView. I already have all the xml items set up, I just don’t know how to write the java code.
I want to have a Button that when clicked, removes all the checked items
Share
The
ListViewdisplays data that comes from anAdapter. In order to remove items from the view the item needs to be removed from theAdapterand the view notified. In android theAdapternotifies the view by callingnotifyDataSetChanged().How to remove an item from the adapter depends on your particular adapter. The
SimpleCursorAdaptergets its data from an underlyingCursor. To remove an item, the item should be removed from the underlyingCursor. For example using aSQLiteCursora row in the database needs to be deleted.If you use the
ArrayAdapterjust callremove(T object)on the adapter. It will automagically callnotifyDataSetChanged()for you.update:
I saw the code at git hub. Here are some pointers as how to get your app working as soon as possible.
Try refactoring your code in to smaller graspable parts. Start with extracting some methods to give parts of the large method understandable names.
The problem is that there might by hundreds of rows in the database and only enough views to fill the screen. Nowhere is it remebered what rows are checked, hence its not possible to remove them. You probably need to extend BaseAdapter or
SimpleCursorAdapterto hold the state (checked or not) of the rows. Read up on the excellent android documentation.My point here is there is a distinction between the view, your
CheckBox, and the model containing the data to display. So check out Model-View-Controller. You can ignore the concept of controller for now.