Tutorial CRUD android SQlite Part II

Tampilan edit data

Bagian kedua ini adalah lanjutan dari tutorial CRUD Sqlite di android. Pada bagian sebelumnya kita sudah membahas tentang cara menampilkan data sqlite ke listview dan cara menambah data. Pada bagian kedua ini kita fokus pada dua hal yaitu Edit data dan delete data.

Tampilan Edit

Tampilan edit data
Tampilan edit data

File yang dibutuhkan

Untuk menambahkan fitur edit kedalam aplikasi, kita membutuhkan 2 file baru dan beberapa perbuahan di DBController.java.
File layout xml activity_edit_kampus.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCCCCC"
    android:orientation="vertical"
    android:paddingTop="1dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:padding="5dp"
        android:text="Edit Kampus"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:orientation="vertical"
            android:padding="10dp" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="30dp"
        android:text="nama kampus"
        android:textColor="#000" />

    <EditText
        android:id="@+id/txtKampusName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:ems="10" >

        <requestFocus />
    </EditText>

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="112dp"
            android:layout_marginRight="48dp"
            android:onClick="removeKampus"
            android:text="Delete" />

        <Button
            android:id="@+id/btnEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/btnDelete"
            android:layout_alignBottom="@+id/btnDelete"
            android:layout_toRightOf="@+id/textView2"
            android:onClick="editKampus"
            android:text="Edit" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtKampusName"
            android:layout_below="@+id/txtKampusName"
            android:text="alamat"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000" />

        <EditText
            android:id="@+id/txtEditAlamat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView3"
            android:layout_below="@+id/textView3"
            android:ems="10" />

    </RelativeLayout>

</LinearLayout>

Kode EditKampusActivity.java

File ini bertugas untuk menampilkan form edit kampus, memanggil fungsi updateKampus.

package com.candra.sqlite;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class EditKampusActivity extends Activity{
	EditText txtKampusName,txtEditAlamat;
	DBController controller = new DBController(this);
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
		 	super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_edit_kampus);
			txtKampusName = (EditText) findViewById(R.id.txtKampusName);
			txtEditAlamat = (EditText) findViewById(R.id.txtEditAlamat);
			Intent objIntent = getIntent();
			String kampusId = objIntent.getStringExtra("kampusId");
			Log.d("Reading: ", "Reading all kampus..");
			HashMap<String, String> kampusList = controller.getKampusInfo(kampusId);
			Log.d("kampusName",kampusList.get("kampusName"));
			if(kampusList.size()!=0) {
			txtKampusName.setText(kampusList.get("kampusName"));
			txtEditAlamat.setText(kampusList.get("alamat"));
			}
	    }
	public void editKampus(View view) {
		HashMap<String, String> queryValues =  new  HashMap<String, String>();
		txtKampusName = (EditText) findViewById(R.id.txtKampusName);
		txtEditAlamat = (EditText) findViewById(R.id.txtEditAlamat);
		Intent objIntent = getIntent();
		String kampusId = objIntent.getStringExtra("kampusId");
		queryValues.put("kampusId", kampusId);
		queryValues.put("kampusName", txtKampusName.getText().toString());
		queryValues.put("alamat", txtEditAlamat.getText().toString());
		
		controller.updateKampus(queryValues);
		this.callHomeActivity(view);
		
	}
	public void removeKampus(View view) {
		Intent objIntent = getIntent();
		String kampusId = objIntent.getStringExtra("kampusId");
		controller.deleteKampus(kampusId);
		this.callHomeActivity(view);
		
	}
	public void callHomeActivity(View view) {
		Intent objIntent = new Intent(getApplicationContext(), MainActivity.class);
		startActivity(objIntent);
	}
}

Selain itu, kita perlu menambahkan 3 fungsi ke DBController.java . Fungsinya adalah fungsi delete, update dan getKampusInfo. Fungsi getKampusInfo dipakai untuk mengambil 1 data kampus berdasarkan idkampusnya.

public int updateKampus(HashMap<String, String> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();	 
	    ContentValues values = new ContentValues();
	    values.put("kampusName", queryValues.get("kampusName"));
	    values.put("alamat", queryValues.get("alamat"));
	    return database.update("kampus", values, "kampusId" + " = ?", new String[] { queryValues.get("kampusId") });
	}
	
	public void deleteKampus(String id) {
		Log.d(LOGCAT,"delete");
		SQLiteDatabase database = this.getWritableDatabase();	 
		String deleteQuery = "DELETE FROM  kampus where kampusId='"+ id +"'";
		Log.d("query",deleteQuery);		
		database.execSQL(deleteQuery);
	}
	public HashMap<String, String> getKampusInfo(String id) {
		HashMap<String, String> kampusList = new HashMap<String, String>();
		SQLiteDatabase database = this.getReadableDatabase();
		String selectQuery = "SELECT * FROM kampus where kampusId='"+id+"'";
		Cursor cursor = database.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
	        do {
					//HashMap<String, String> map = new HashMap<String, String>();
	        	kampusList.put("kampusName", cursor.getString(1));
	        	kampusList.put("alamat", cursor.getString(2));
				   //wordList.add(map);
	        } while (cursor.moveToNext());
	    }				    
	return kampusList;
	}	

Bagian terkahir, tambahkan event clickListener kedalam MainActivity.java agar saat saat listview di klik, program akan membuka Form edit data dengan mengirimkan idKampusnya.

lv.setOnItemClickListener(new OnItemClickListener() {
				  @Override 
				  public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					  kampusId = (TextView) view.findViewById(R.id.kampusId);
					  String valkampusId = kampusId.getText().toString();					  
					  Intent  objIndent = new Intent(getApplicationContext(),
							  EditKampusActivity.class);
					  objIndent.putExtra("kampusId", valkampusId); 
					  startActivity(objIndent); 
				  }
			}); 

Download

Silahkan coba dan download sendiri project lengkapnya disini.

Author: Candra Adi Putra

Candra Adi Putra adalah Alumni STMIK AKAKOM Yogyakarta. Like Candralab Studio Di Facebook.

3 thoughts on “Tutorial CRUD android SQlite Part II”

Tinggalin komentar dong!