Skip to content
Advertisement

How to automatically update ListView with custom adapter when item is deleted from database

I created a ListView for which I implemented a custom adapter. Additionally I connected it to a SQLite database, which contains the content for the ListView.

I have one ListView with several items. Every item consists of a TextView and an ImageView which functions as a button. When the user clicks the ImageView, I want to delete the item from the database and automatically update the ListView.

Unluckily, I just know how to delete the item from the database. I can’t think of a way to automatically update the ListView in my program, after the item got deleted from the database.

In the version below, I added an onItemClickListener for the ListView in the MainActivity – but it doesn’t function for when the user clicks the ImageView (even though the ImageView is part of the ListView).

MainActivity

JavaScript

}

DataModel

JavaScript

}

DataAdapter

JavaScript

}

DataBaseHelper

JavaScript

}

I appreciate any kind of help or suggestion. Let me know, if you need further explanation.

Nicole

Advertisement

Answer

In the version below, I added an onItemClickListener for the ListView in the MainActivity – but it doesn’t function for when the user clicks the ImageView (even though the ImageView is part of the ListView).

Consider the following based upon your code:-

Working Example

The following code is based upon yours BUT with various changes that does delete and refresh the list when the image (purple box is clicked).

The list_item.xml layout (altered to highlight the items as no image):-

JavaScript

The DataModel class is unchanged.

The DatabaseHelper class has been changed to a) not close the database (this is inefficient as it will apply the WAL changes and then have to subseuqently open the databaset which is relatively resource costly) and b) to close all cursors when done with the and c) utilise the convenience methods :-

JavaScript

DataAdapter

JavaScript
  • NOTE an important aspect is utilising the tag to tie the clicked view to the Todo/DataModel.
  • NOTE another important factor is the manipulation of the DataAdapter’s instance of the ArrayList and the subsequent notifyDatasetChanged that tells the adapter to rebuild.

and MainActivity :-

JavaScript

*Results

From a new install (empty Database) then the following actions were taken:-

  1. Add Test001 – Test006 so :-

    1. enter image description here
  2. Click on Item Test003 (NOT the purple box (image))

    1. Toast displays as expected.
    2. Log includes D/CLICKACTION: Item was clicked for TitleTest003
  3. Click on the Delete image (purple box) for Test003

    1. Toast displays as expected Test003 position 2
    2. Log includes D/CLICKACTION: Image clicked for title = Test003 position 2
    3. enter image description here
      1. i.e. Test003 removed from the display
    4. Database via App Inspection shows:-
    • enter image description here
      • i.e. no Test003 row in the database
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement