Skip to main content

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

Comments

Popular posts from this blog

Several English proverbs and the Malay pair

Or you could download here for the Malay proverbs app – https://play.google.com/store/apps/details?id=net.kerul.peribahasa English proverbs and the Malay pair Corpus Reference: Amir Muslim, 2009. Peribahasa dan ungkapan Inggeris-Melayu. DBP, Kuala Lumpur http://books.google.com.my/books/about/Peribahasa_dan_ungkapan_Inggeris_Melayu.html?id=bgwwQwAACAAJ CTRL+F to search Proverbs in English Definition in English Similar Malay Proverbs Definition in Malay 1 Where there is a country, there are people. A country must have people. Ada air adalah ikan. Ada negeri adalah rakyatnya. 2 Dry bread at home is better than roast meat home's the best hujan emas di negeri orang,hujan batu di negeri sendiri Betapa baik pun tempat orang, baik lagi tempat sendiri. 3 There's no accounting for tastes We can't assume that every people have a same feel Kepala sama hitam hati lain-lain. Dalam kehidupan ini, setiap insan berbeza cara, kesukaan, perangai, tabia...

Submit your blog address here

Create your own blog and send the address by submitting the comment of this article. Make sure to provide your full name, matrix and URL address of your blog. Refer to the picture below. Manual on developing a blog using blogger.com and AdSense, download here … Download Windows Live Writer (a superb offline blog post editor)

Applications of Web 2.0

Web 2.0 describes the changing trends in the use of World Wide Web technology and web design that aim to enhance creativity , secure information sharing, collaboration and functionality of the web. Web 2.0 concepts have led to the development and evolution of web-based communities and hosted services , such as social-networking sites , video sharing sites , wikis , blogs . Find a website or web application that conform to the criteria of Web 2.0. Put the name of the application and the URL in the comment below. Please provide your full name and matrix number. Make sure the application you choose is not already chosen by your friend in the previous comment.

Contact Us at blog.kerul.net

Powered by EMF HTML Contact Form

The Challenges of Handling Proverbs in Malay-English Machine Translation – a research paper

*This paper was presented in the 14th International Conference on Translation 2013 ( http://ppa14atf.usm.my/ ). 27 – 29 August 2013, Universiti Sains Malaysia, Penang. The PDF version is here: www.scribd.com/doc/163669571/Khirulnizam-The-Challenges-of-Automated-Detection-and-Translation-of-Malay-Proverb The test data is here: http://www.scribd.com/doc/163669588/Test-Data-for-the-Research-Paper-the-Challenges-of-Handling-Proverbs-in-Malay-English-Machine-Translation Khirulnizam Abd Rahman, Faculty of Information Science & Technology, KUIS Abstract: Proverb is a unique feature of Malay language in which a message or advice is not communicated indirectly through metaphoric phrases. However, the use of proverb will cause confusion or misinterpretation if one does not familiar with the phrases since they cannot be translated literally. This paper will discuss the process of automated filtering of Malay proverb in Malay text. The next process is translation. In machine translatio...