2 Komentar

Aplikasi Android CRUD Sederhana

Kali ini saya akan membuat aplikasi mobile android CRUD ( Create Update Delete ) sederhana yaitu input biodata yang menggunakan java eclipe dan database SQLLite, berikut step by step pembuatanya

pertama buat dulu project android aplikasinya dengan nama biodata

new project

kemudian tekan next sampai finish.
jika sudah finish buat 4 layout activity yaitu activity_main ,  activity_baru , activity_lihat , activity_ubah

aplikasi

activity_baru

activity_ubah

activity_lihat

setelah di buat layoutnya maka kita buat source code nya
MainActivity.java

package com.example.aplikasicrud;

import android.os.Bundle;
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.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
	String[] daftar; 
	ListView ListView01;
	Menu menu;
	protected Cursor cursor;
	DataCenter dbcenter;
	public static MainActivity ma;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button ton=(Button)findViewById(R.id.button2);
		Button back=(Button)findViewById(R.id.button1);
		ton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent inte = new Intent(MainActivity.this, BaruActivity.class);
				startActivity(inte);
			}
		});
		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		ma = this;
        dbcenter = new DataCenter(this);
        RefreshList();
	}
	
	public void RefreshList(){
    	SQLiteDatabase db = dbcenter.getReadableDatabase();
    	cursor = db.rawQuery("SELECT * FROM biodata",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.listView1);
    	ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));
    	ListView01.setSelected(true);
    	ListView01.setOnItemClickListener(new OnItemClickListener() {
    
    		
    		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    			final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
    			final CharSequence[] dialogitem = {"View", "Edit", "Delete"};
    			AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    			builder.setTitle("Pilih Menu");
    			builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
    				public void onClick(DialogInterface dialog, int item) {
    					switch(item){
    					case 0 :
    						Intent i = new Intent(getApplicationContext(), LihatActivity.class);
    						i.putExtra("nama", selection);
    						startActivity(i);
    						break;
    					case 1 :
    						Intent in = new Intent(getApplicationContext(), UbahActivity.class);
    						in.putExtra("nama", selection);
    						startActivity(in);
    						break;
    					case 2 :
    						SQLiteDatabase db = dbcenter.getWritableDatabase();
    						db.execSQL("delete from biodata where nama = '"+selection+"'");
    						RefreshList();
    						break;
    					}
    				}
    			});
    			builder.create().show();
    		}});
    		((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();
    	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

BaruActivity.java

package com.example.aplikasicrud;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class BaruActivity extends Activity {
	protected Cursor cursor;
	DataCenter dbHelper;
	Button ton1, ton2;
	EditText text1, text2, text3, text4, text5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baru);
		
		dbHelper = new DataCenter(this);
		text1 = (EditText) findViewById(R.id.editText1);
		text2 = (EditText) findViewById(R.id.editText2);
		text3 = (EditText) findViewById(R.id.editText3);
		text4 = (EditText) findViewById(R.id.editText4);
		text5 = (EditText) findViewById(R.id.editText5);
		ton1 = (Button) findViewById(R.id.button1);
		ton2 = (Button) findViewById(R.id.button2);
		// daftarkan even onClick pada btnSimpan
		ton1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				db.execSQL("insert into biodata(kode, nama, tgl, jk, alamat) values('" +
						text1.getText().toString()+"','"+
						text2.getText().toString() +"','" +
						text3.getText().toString()+"','"+
						text4.getText().toString() +"','" +
						text5.getText().toString() + "')");
						Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
						MainActivity.ma.RefreshList();
						finish();
				}
			});
		ton2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.baru, menu);
		return true;
	}

}

LihatActivity.java

package com.example.aplikasicrud;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LihatActivity extends Activity {
	protected Cursor cursor;
	DataCenter dbHelper;
	Button ton2;
	TextView text1, text2, text3, text4, text5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lihat);
		
		dbHelper = new DataCenter(this);
		text1 = (TextView) findViewById(R.id.textView1);
		text2 = (TextView) findViewById(R.id.textView2);
		text3 = (TextView) findViewById(R.id.textView3);
		text4 = (TextView) findViewById(R.id.textView4);
		text5 = (TextView) findViewById(R.id.textView5);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		cursor = db.rawQuery("SELECT * FROM biodata WHERE nama = '" +
		getIntent().getStringExtra("nama") + "'",null);
		cursor.moveToFirst();
		if (cursor.getCount()>0)
		{
			cursor.moveToPosition(0);
			text1.setText(cursor.getString(0).toString());
			text2.setText(cursor.getString(1).toString());
			text3.setText(cursor.getString(2).toString());
			text4.setText(cursor.getString(3).toString());
			text5.setText(cursor.getString(4).toString());
		}
		ton2 = (Button) findViewById(R.id.button1);
		ton2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lihat, menu);
		return true;
	}

}

UbahActivity.java

package com.example.aplikasicrud;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UbahActivity extends Activity {
	protected Cursor cursor;
	DataCenter dbHelper;
	Button ton1, ton2;
	EditText text1, text2, text3, text4, text5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ubah);
		
		dbHelper = new DataCenter(this);
		text1 = (EditText) findViewById(R.id.editText1);
		text2 = (EditText) findViewById(R.id.editText2);
		text3 = (EditText) findViewById(R.id.editText3);
		text4 = (EditText) findViewById(R.id.editText4);
		text5 = (EditText) findViewById(R.id.editText5);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		cursor = db.rawQuery("SELECT * FROM biodata WHERE nama = '" +
		getIntent().getStringExtra("nama") + "'",null);
		cursor.moveToFirst();
		if (cursor.getCount()>0)
		{
			cursor.moveToPosition(0);
			text1.setText(cursor.getString(0).toString());
			text2.setText(cursor.getString(1).toString());
			text3.setText(cursor.getString(2).toString());
			text4.setText(cursor.getString(3).toString());
			text5.setText(cursor.getString(4).toString());
		}
		ton1 = (Button) findViewById(R.id.button1);
		ton2 = (Button) findViewById(R.id.button2);
		// daftarkan even onClick pada btnSimpan
		ton1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				db.execSQL("update biodata set nama='"+
						text2.getText().toString() +"', tgl='" +
						text3.getText().toString()+"', jk='"+
						text4.getText().toString() +"', alamat='" +
						text5.getText().toString() + "' where kode='" +
						text1.getText().toString()+"'");
						Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
						MainActivity.ma.RefreshList();
						finish();
				}
			});
		ton2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ubah, menu);
		return true;
	}

}

DataCenter.java

package com.example.aplikasicrud;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataCenter extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "crud.db";
	private static final int DATABASE_VERSION = 1;
	public DataCenter(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "create table biodata(kode integer primary key, nama text null, tgl text null, jk text null, alamat text null);";
		Log.d("Data", "onCreate: " + sql);
		db.execSQL(sql);
		sql = "INSERT INTO biodata (kode, nama, tgl, jk, alamat) VALUES ('1255', 'Ghazali', '1992-10-11', 'Laki-laki','Grong-grong');";
		db.execSQL(sql);
		sql = "INSERT INTO biodata (kode, nama, tgl, jk, alamat) VALUES ('1156', 'Andre', '1994-06-14', 'Laki-laki','Kembang Tanjong');";
		db.execSQL(sql);
		sql = "INSERT INTO biodata (kode, nama, tgl, jk, alamat) VALUES ('1357', 'Diska', '1992-07-04', 'Perempuan','Sigli');";
		db.execSQL(sql);
		sql = "INSERT INTO biodata (kode, nama, tgl, jk, alamat) VALUES ('1456', 'Intan', '1992-06-14', 'Perempuan','Caleue');";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

}

kemudian setelah selesai program di jalankan dan akan tampil seperti dibawah ini

tampilanawal

tampilan awal aplikasi, bersisi data biodata yang telah disimpan bisa melakukan penambahan, view , update dan delete dengan cara meng klick pada salah satu data yang dipilih maka akan tampil menu pilihan

menu

tambah data

tambahdata

lihat data yang sudah di tambah

lihatdata

 

demikian tutorial pembuatan aplikasi sederhana dengan android semoga bermanfaat..

2 comments on “Aplikasi Android CRUD Sederhana

  1. kalau mau diubah ke database phpmysql bagaimana ya pak?

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: