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

Add new Record - Android database SQLite (p7)

Tutorial description: The product of this tutorial is to invoke (from MENU) another screen to insert a new record. The first screen is the list of all records. User is able to add a new record through this screen. The screenshots below is the output.

add-new-record screenshot1add-new-record screenshot2add-new-record screenshot3

Before you proceed with the tutorial, you might want to read the previous tutorial - Update Record by long-press ListView - Android database SQLite (p6)

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

STEP 1: Add a new screen named AddNewRecord

Right-click on the project name –>New –> Other… Select the Android Activity –> Blank Activity. Provide the new Activity (screen) name AddNewRecord

add new screen to display complete record

new screen select Android ctivity

Provide the Activity Name as AddNewRecord, and hit Finish

new activity add-new-record

Update Record by long-press ListView - Android database SQLite (p6)

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

screen offline db output display all records -> long-press on selected item and the UpdateRecord screen will appear screen offline db update record

Tutorial description: The product of this tutorial is to invoke another screen to update (edit) the selected record. The first screen is the list of all records. User is able to update the selected record by long-pressing the item.

Before you proceed with the tutorial, you might want to read the previous tutorial - View Complete Record from ListView.

 

STEP 1: Add a new screen named UpdateRecord

Right-click on the project name –>New –> Other…

add new screen to display complete record

new screen select Android ctivity

Set the Activity Name as UpdateRecord and hit Finish…

new activity update-record

View Complete Record from ListView - Android database SQLite (p5)

This tutorial is a continuation of tutorial on Populate Records in ListView

This exercise will invoke another screen to display complete information of the selected staff from the ListView.

screen offline db display all data

Code is here - http://khirulnizam.com/android/FSTMDirektori.zip

STEP 1: Add a new screen (to display complete info of staff).

Right-click the project, New –> Other…

right click package and create new class

new screen select Android ctivity

Populate Records in ListView - Android database SQLite (p4)

For this exercise, we will be working with and offline database, which has a table name fstmdirektori (stafid, nama, jbt, telefon, emel).

screen offline db output display all recordsThis is what we will produce.

Code is here - http://khirulnizam.com/android/FSTMDirektori.zip

Step 1: Create a new blank Android project, let say named FSTM Direktori.

create new project named FSTM Direktori

Step 2: Add ListView in the main screen.

add listview in main layout

Popular Posts

WorldConferences.net