Android SQLite delete row

When you are on this page, I assume you have read the Android SQLite example tutorial that shows you how to create a database and a table, insert data into the table, and load the data in the ListView. In this post, I am going to show you how to delete a row from the table.


Deleting a row from the able can be done by using the delete(String table_name, String whereClause,String[] whereArgs) of the SQLiteDatabase. This method has three arguments: table_name, whereClause, and whereArgs. The table_name is the name of the table from which the row is deleted. The whereClause is the optional WHERE clause to apply when deleting. Passing null will delete all rows. You may include ?s in the where clause, which will be replaced by the values from whereArgs.

Now to have a workable example of deleting row from SQLite database, you will modify the previous Android project (in Android SQLite example tutorial page). First you edit the list_item.xml file to add one Button as shown below. The Button is clicked to delete a row that the Button is on.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="5dp" >
  
<TextView
     android:id="@+id/pro_name"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_marginRight="10dp"
     android:textSize="20sp"
       />

<TextView
     android:id="@+id/pro_uprice"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
android:textSize="20sp"
android:layout_toRightOf="@+id/pro_name"
      />
<Button
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Delete"
     android:textColor="#ff0000"
     android:layout_alignParentRight="true"
     android:onClick="deleteRow"
   />
  
</RelativeLayout>



After that, you will modify the CustomAdapter class to create a reference to the Button and set a value of the data key field to its tag using the setTag method. When a button of a row is clicked, the value of its tag is retrieved to be used in the delete method of the SQLiteDatabase instance. The edited CustomAdapter is shown below.

  package com.example.androidproject;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> {

int groupid;
ArrayList<String> records;
Context context;

public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;

}

public View getView(int position, View convertView, ViewGroup parent) {

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View itemView = inflater.inflate(groupid, parent, false);
     String[] row_items=records.get(position).split("__");
     TextView textName= (TextView) itemView.findViewById(R.id.pro_name);
     textName.setText(row_items[1]);
     TextView textPrice= (TextView) itemView.findViewById(R.id.pro_uprice);
     textPrice.setText(row_items[2]+"$");
     Button bt_del=(Button)itemView.findViewById(R.id.bt_delete);
     bt_del.setTag(row_items[0]);
     return itemView;
}
}

In the MainActivity class, you will add a new method. The name of the method is deleteRow. It will be called to delete a row when the button on that row is clicked.

public void deleteRow(View view){
Button bt=(Button)view;
SQLiteDatabase database=dbhelper.getWritableDatabase();
String del_id=bt.getTag().toString();
//delete the row from the database
database.delete(DBSchema.TABLE_NAME, DBSchema.COL_NAME_KEY+"=?", new String[]{del_id});
//delete the row from the records ArrayList
for(int i=0;i<records.size();i++){
if(records.get(i).startsWith(del_id))
records.remove(i);
}
//notify listview of dataset changed
   adapter.notifyDataSetChanged();

}


Android-SQLite-delete-row

Posted by: Dara | post date: 07-31-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.