Delete a record from a ListView–Android database SQLite (p8)

This tutorial is the continuation of the tutorial in Add new Record

By long-pressing a record, user will invoke the Dialog box. Choose DELETE to confirm deleting the selected record. Choosing UPDATE will invoke the update record screen.

delete-record-screenshot1delete-record-screenshot2delete-record-screenshot3

Download the complete code here  ic_file_download_black_24dp_2x http://khirulnizam.com/android/FSTMDirektori.zip

STEP 1: Adjusting the onItemLongClickListener to accommodate the deleting a record facilities.

delete-record update-alert-dialog

delete-record delete-alert-dialog

The updated DisplayList.java with the delete record function

package net.kerul.fstmdirektori;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class DisplayList extends Activity {

private String id;//to hold selected stafid
private DBHelper mHelper;
private SQLiteDatabase dataBase;

//variables to hold staff records
private ArrayList<String> stafid = new ArrayList<String>();
private ArrayList<String> nama = new ArrayList<String>();
private ArrayList<String> jbt = new ArrayList<String>();

private ListView userList;
private AlertDialog.Builder build;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_list);

userList = (ListView) findViewById(R.id.List);

mHelper = new DBHelper(this);

//click to update data
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

Intent i = new Intent(getApplicationContext(),
ViewRecord.class);
i.putExtra("stafid", stafid.get(arg2));
startActivity(i);
}
});//end setOnItemClickListener

//long-press to update data
userList.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {

//invoking AlertDialog box
build = new AlertDialog.Builder(DisplayList.this);
build.setTitle("Update/Delete staff " + nama.get(arg2));
build.setMessage("Do you want to update/delete the record?(Hit back to cancel)");

//user select UPDATE
build.setNegativeButton("UPDATE",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
//Update record selected
Intent i = new Intent(getApplicationContext(),
UpdateRecord.class);
i.putExtra("stafid", stafid.get(arg2));
startActivity(i);
dialog.cancel();
}
});//end UPDATE

//user select DELETE
build.setPositiveButton("DELETE",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,int which) {
dataBase.delete(
DBHelper.TABLE_NAME,
DBHelper.STAFID + "="
+ stafid.get(arg2), null);
Toast.makeText(
getApplicationContext(),
nama.get(arg2)+
" is deleted.", Toast.LENGTH_SHORT).show();
displayData();
dialog.cancel();
}
});//end DELETE
AlertDialog alert = build.create();
alert.show();

return true;
}
});//end setOnItemLongClickListener


}// end onCreate method

@Override
protected void onResume() {
//refresh data for screen is invoked/displayed
displayData();
Toast.makeText(getApplicationContext(), "TAP to view complete info, \n LONG-TAP to update/delete...", Toast.LENGTH_LONG).show();
super.onResume();
}

/**
* displays data from SQLite
*/
private void displayData() {
dataBase = mHelper.getWritableDatabase();
//the SQL command to fetched all records from the table
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DBHelper.TABLE_NAME, null);

//reset variables
stafid.clear();
nama.clear();
jbt.clear();

//fetch each record
if (mCursor.moveToFirst()) {
do {
//get data from field
stafid.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.STAFID)));
nama.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.NAMA)));
jbt.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.JBT)));

} while (mCursor.moveToNext());
//do above till data exhausted
}

//display to screen
DisplayAdapter disadpt = new DisplayAdapter(DisplayList.this, stafid, nama, jbt);
userList.setAdapter(disadpt);
mCursor.close();
}//end displayData




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.getItemId()==R.id.m_add_record) {

//invoke the AddNewRecord screen using Intent
Intent i = new Intent(getApplicationContext(),
AddNewRecord.class);
startActivity(i);
}
return true;
}

}//end DisplayList class

Popular Posts

WorldConferences.net