Rabu, 09 Januari 2013

Aplikasi Android Menghapus Data, Delete pada Database SQLite


Tulisan ini adalah sambungan dari artikel sebelumnya yaitu Membuat Aplikasi Android untuk Mengedit / Update Data ke Database SQLite. Silakan download atau ikuti pembuatan program dari Artikel tersebut untuk kemudian kita tambahkan kemampuan Android Menghapus / Delete Data di Database SQLite.

Pada Program utama di MainActivity kita tambahkan beberapa baris program untuk melakukan Delete Hapus Data :

MainActivity.java

package com.amijaya.databasesqlite;

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.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

// http://cariprogram.blogspot.com
// nuramijaya@gmail.com

public class MainActivity extends Activity {
String[] daftar; // = {"Wisata Candi", "Wisata Pantai", "Wisata Alam"};
ListView ListView01;
Menu menu;
protected Cursor cursor;
SQLHelper dbHelper;
public static MainActivity ma;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ma = this;
        
        dbHelper = new SQLHelper(this);

RefreshList();
    }
    

    public void RefreshList()
    {
    SQLiteDatabase db = dbHelper.getReadableDatabase();

cursor = db.rawQuery("SELECT * FROM kota",null);

daftar = new String[cursor.getCount()];
cursor.moveToFirst();
for (int cc=0; cc < cursor.getCount(); cc++)
{
cursor.moveToPosition(cc);
daftar[cc] = cursor.getString(1).toString();
}

    ListView01 = (ListView)findViewById(R.id.ListView01);
        ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));
        ListView01.setSelected(true);
        ListView01.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
    final CharSequence[] dialogitem = {"Edit", "Delete"};
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
       builder.setTitle("Pilih ?");
       builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0 :
Intent i = new Intent(getApplicationContext(), EditActivity.class); //KulineryogyaActivity.class); 
i.putExtra("nama", selection);
    startActivity(i);

break;
case 1 :
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL("delete from kota where nama = '"+selection+"'");
       RefreshList();
break;
}
}
});
       builder.create().show();
}});

        
        ((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();   
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
   
    menu.add(0, 1, 0, "Tambah").setIcon(android.R.drawable.btn_plus);
    menu.add(0, 2, 0, "Refresh").setIcon(android.R.drawable.ic_menu_rotate);
        menu.add(0, 3, 0, "Exit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
        Intent i = new Intent(MainActivity.this, AddActivity.class);
        startActivity(i);
            return true;
        case 2:            
            RefreshList();         
    return true;
        case 3:
            finish();
            return true;
        }
    return false;
    }

}

Selanjutnya tidak ada lagi perubahan baik pada file layout .xml maupun AndroidActivity.xml

Hasilnya :





Project selengkapnya silakan didownload disini :
https://docs.google.com/open?id=0B4i1FYc_4RXzRk04d0NhQlE4OGs

Semoga bermanfaat, Selamat Coding.