Android SQLite How to Delete Rows In Database

UPDATE: See Complete Application Example Here

Here we will delete SQLite database rows, which we inserted in previous post . For SQLite integration see this post if you are at the beginning of using the database in your application.

So as we already have application using which we can insert rows in the database.

Previous Posts:

Part 1: Android SQLite: Integration.
Part 2: How to Insert New Row in SQLite Database.

Following Post will also answer:

  • How to Create a ListView in Android Activity with multiple values?
  • How to Remove a row from ListView in Android.
  • How to Refresh ListView after a row is removed?

Let’s start creating a view to deleting rows from the table.

Step 1) In MainActivity.java we will add a new button open a new activity (deleteRowActivity) with ListView having a list of rows in the table with the button to delete a single row.

 

Add method deleteRowActivity in MainActivity

This method will open deleteRowActivity.

 

Step 2) Create new activity deleteRowActivity, right click on root package > New >Activity > Empty Activity

Add below code in deleteRowActivity we created

This activity is only having code to back arrow in Status Bar. Now we will create a ListLayout to show the list of items in this activity.

Add the following layout in activity_delete_rows.xml in res folder

 

Step 3) Create ListView, Right click on res folder > New > Layout resource file

Add below code in listviewdelete_row.xml

This layout will act as a template for listview we will show in activity.

Step 4) Create a custom list adapter (CustomListAdapterDeleteRows) which will extend BaseAdapter

Constructor function CustomListAdapterDeleteRows will take User data input which we will send from deleteRowActivity.

Here we are adding a click listener on DEL button to delete row using deleteEntry method then updating ListView after removing the item at the current position.

 

Step 5) We will now create a User Modal Class which will have getters and setters for user data.

 

Step 6) Add getRows method UsersDatabaseAdapter (See previous post)

Find Source Code Up to this exercise here

Now application will have Delete Row button in main activity which will move to other activity with ListView of all rows in Table and a DEL button to delete each row.

Leave a Reply

Your email address will not be published. Required fields are marked *