Delete Queries
By Fanxtea | 1 Januari 2015Tutorial ini akan menunjukkan bagaimana user menggunakan delete queries untuk memadam data yang telah disimpan di dalam pangkalan data. Pangkalan data yang digunakan di dalam tutorial ini adalah daripada tutorial sebelom ini iaitu tutorial insert queries.
- Android Layout Buka fail res/layout/activity_main.xml, tambah satu senarai, satu butang dan dua komponen Edit Text, satu untuk nama dan satu lagi alamat, untuk disimpan ke dalam pangkalan data.
- Contact class Fail : Contact.java
- Kod Fail: MainActivity.java
- Demo Run aplikasi.
Fail: res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/textViewName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="@+id/textViewAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextAddress" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/btnDelete" />
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</LinearLayout>
package com.fanxtea.helloworld;
public class Contact {
//private variables
int _id;
String _name;
String _address;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String name, String _address){
this._id = id;
this._name = name;
this._address = _address;
}
// constructor
public Contact(String name, String _address){
this._name = name;
this._address = _address;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getAddress(){
return this._address;
}
// setting phone number
public void setAddress(String _address){
this._address = _address;
}
}
package com.fanxtea.helloworld;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener {
private EditText editTextName;
private EditText editTextAdd;
private Button btnDelete;
private String idName;
private ArrayAdapter adapter;
private ListView listView ;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDatabase();
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAdd = (EditText) findViewById(R.id.editTextAddress);
btnDelete = (Button) findViewById(R.id.btnDelete);
listView = (ListView) findViewById(R.id.list);
btnDelete.setOnClickListener(this);
List contact = this.getAllContacts();
List your_array_list = new ArrayList();
for (Contact cn : contact) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Address: " + cn.getAddress();
// Writing Contacts to log
Log.d("Name: ", log);
your_array_list.add(cn.getID()+") "+cn.getName()+"\n---\n"+cn.getAddress());
}
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, your_array_list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
String pattern = "\\r?\\n\\-\\-\\-\\r?\\n";
String[] temp;
temp = itemValue.split(pattern);
System.out.println("Return Value :" );
for(int i =0; i < temp.length ; i++){
editTextName.setText(temp[0].substring(3));
editTextAdd.setText(temp[1]);
idName= temp[0].substring(0,1);
}
}
});
}
protected void createDatabase(){
db=openOrCreateDatabase("PersonDB.db", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");
}
protected void deleteFromDB(){
String name = editTextName.getText().toString();
String query = "DELETE FROM persons WHERE name='"+name+"'";
db.execSQL(query);
Toast.makeText(getApplicationContext(),"Deleted Successfully", Toast.LENGTH_LONG).show();
}
protected List getAllContacts() {
List contactList = new ArrayList();
// Select All Query
String selectQuery = "SELECT * FROM persons";
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setAddress(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
@Override
public void onClick(View v) {
if(v == btnDelete){
deleteFromDB();finish();
startActivity(getIntent());
}
}
}
Klik pada salah satu senarai data yang ada
klik butang Delete dan data tersebut akan dipadam.