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.
<?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>
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();
}
Posted by: Dara | post date: 07-31-2014 | Subject: Android Apps Development
|
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.