Pertama-tama dan yang paling utama buatlah project baru dengan nama terserah anda dan nama package com.example.dbs. Kemudian pada folder res/layout buat file xml baru seperti dibawah ini.
add_update_screen.xml
<RelativeLayout 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:background="@drawable/wall"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/alamattext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/namatext"
android:layout_below="@+id/alamat"
android:layout_marginTop="29dp"
android:text="JENIS KELAMIN"
android:textColor="#ff0000"
android:textStyle="bold" />
<TextView
android:id="@+id/textnim1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nim"
android:layout_alignBottom="@+id/nim"
android:layout_alignParentLeft="true"
android:text="NPM"
android:textColor="#ff0000"
android:textStyle="bold" />
<TextView
android:id="@+id/nimtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textnim1"
android:layout_below="@+id/nim"
android:layout_marginTop="14dp"
android:text="NAMA"
android:textColor="#ff0000"
android:textStyle="bold" />
<EditText
android:id="@+id/alamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/namatext"
android:layout_alignBottom="@+id/namatext"
android:layout_alignRight="@+id/nama"
android:layout_toRightOf="@+id/alamattext"
android:ems="10"
android:hint="Alamat"
android:inputType="textPersonName"
android:textColor="#000000"
android:textColorHint="#ffffff" />
<TextView
android:id="@+id/namatext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nimtext"
android:layout_below="@+id/nama"
android:layout_marginTop="28dp"
android:text="ALAMAT"
android:textColor="#ff0000"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/sexsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/alamat"
android:layout_alignTop="@+id/alamattext"
android:layout_toRightOf="@+id/alamattext" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="Laki-laki"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perempuan"
android:textColor="#000000" />
</RadioGroup>
<EditText
android:id="@+id/nim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nama"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Nim"
android:inputType="textPersonName"
android:maxLength="13"
android:textColor="#000000"
android:textColorHint="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nimtext"
android:layout_alignBottom="@+id/nimtext"
android:layout_toRightOf="@+id/alamattext"
android:ems="10"
android:hint="Nama"
android:inputType="textPersonName"
android:textColor="#000000"
android:textColorHint="#ffffff" />
<Spinner
android:id="@+id/agama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/sexsex"
android:layout_alignRight="@+id/sexsex"
android:layout_below="@+id/sexsex"
android:layout_marginTop="15dp"
android:entries="@array/agama" />
<Button
android:id="@+id/tampill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/simpann"
android:layout_alignRight="@+id/agama"
android:layout_alignTop="@+id/button1"
android:background="#000000"
android:text="Show"
android:textColor="#ffffff"
android:textColorLink="#0000ff" />
<Button
android:id="@+id/simpann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/nim"
android:background="#000000"
android:text="Simpan"
android:textColor="#ffffff"
android:textColorLink="#0000ff" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/simpann"
android:layout_alignLeft="@+id/agama"
android:layout_alignTop="@+id/simpann"
android:layout_marginLeft="18dp"
android:background="#000000"
android:text="Reset"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/alamattext"
android:layout_alignTop="@+id/agama"
android:text="AGAMA"
android:textColor="#ff0000"
android:textStyle="bold" />
</RelativeLayout>
listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/user_name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/www"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/npm_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0000ff" />
<TextView
android:id="@+id/nama_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffff00" />
<TextView
android:id="@+id/alamat_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ff0000" />
<TextView
android:id="@+id/kelamin_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00ff00" />
<TextView
android:id="@+id/agama_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#996600" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/write2"
android:focusable="false"
android:focusableInTouchMode="false"
android:minHeight="50dip"
android:minWidth="50dip" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/delet"
android:focusable="false"
android:focusableInTouchMode="false"
android:minHeight="50dip"
android:minWidth="50dip" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="@+id/add_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000"
android:clickable="false"
android:focusable="false"
android:minWidth="64dp"
android:text="Tambah Biodata"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
Pada folder values/strings tambahkan koding berikut untuk item spinner agama.
<string-array name="agama">
<item>Islam</item>
<item>Hindu</item>
<item>Budha</item>
<item>Protestan</item>
<item>Katholik</item>
</string-array>
Kemudian pada folder src/ buatlah file java baru dengan nama seperti dibawah ini.
Main_Screen.java
package com.example.dbs;
import java.util.ArrayList;
import com.example.dbs.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Main_Screen extends Activity {
Button add_btn;
ListView Contact_listview;
ArrayList<Contact> contact_data = new ArrayList<Contact>();
Contact_Adapter cAdapter;
DatabaseHandler db;
String Toast_msg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Contact_listview = (ListView) findViewById(R.id.list);
Contact_listview.setItemsCanFocus(false);
add_btn = (Button) findViewById(R.id.add_btn);
Set_Referash_Data();
} catch (Exception e) {
// TODO: handle exception
Log.e("some error", "" + e);
}
add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent add_user = new Intent(Main_Screen.this,Add_Update_User.class);
add_user.putExtra("called", "Simpan");
add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(add_user);
finish();
}
});
}
public void Set_Referash_Data() {
contact_data.clear();
db = new DatabaseHandler(this);
ArrayList<Contact> contact_array_from_db = db.Get_Contacts();
for (int i = 0; i < contact_array_from_db.size(); i++) {
int tidno = contact_array_from_db.get(i).getID();
String npm = contact_array_from_db.get(i).getNpm();
String nama = contact_array_from_db.get(i).getNama();
String alamat = contact_array_from_db.get(i).getAlamat();
String kelamin = contact_array_from_db.get(i).getKelamin();
String agama = contact_array_from_db.get(i).getAgama();
Contact cnt = new Contact();
cnt.setID(tidno);
cnt.setNpm(npm);
cnt.setNama(nama);
cnt.setAlamat(alamat);
cnt.setKelamin(kelamin);
cnt.setAgama(agama);
contact_data.add(cnt);
}
db.close();
cAdapter = new Contact_Adapter(Main_Screen.this, R.layout.listview_row,
contact_data);
Contact_listview.setAdapter(cAdapter);
cAdapter.notifyDataSetChanged();
}
public void Show_Toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Set_Referash_Data();
}
public class Contact_Adapter extends ArrayAdapter<Contact> {
Activity activity;
int layoutResourceId;
Contact user;
ArrayList<Contact> data = new ArrayList<Contact>();
public Contact_Adapter(Activity act, int layoutResourceId,
ArrayList<Contact> data) {
super(act, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.data = data;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.name = (TextView) row.findViewById(R.id.npm_txt);
holder.email = (TextView) row.findViewById(R.id.nama_txt);
holder.number = (TextView) row.findViewById(R.id.alamat_txt);
holder.kelamn = (TextView) row.findViewById(R.id.kelamin_txt);
holder.agam = (TextView) row.findViewById(R.id.agama_txt);
holder.edit = (Button) row.findViewById(R.id.btn_update);
holder.delete = (Button) row.findViewById(R.id.btn_delete);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
user = data.get(position);
holder.edit.setTag(user.getID());
holder.delete.setTag(user.getID());
holder.name.setText(user.getNpm());
holder.email.setText(user.getNama());
holder.number.setText(user.getAlamat());
holder.kelamn.setText(user.getKelamin());
holder.agam.setText(user.getAgama());
holder.edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Edit data", "**********");
Intent update_user = new Intent(activity,
Add_Update_User.class);
update_user.putExtra("called", "Update");
update_user.putExtra("USER_ID", v.getTag().toString());
activity.startActivity(update_user);
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
// show a message while loader is loading
AlertDialog.Builder adb = new AlertDialog.Builder(activity);
adb.setTitle("Hapus?");
adb.setMessage("Anda yakin ingin menghapus?");
final int user_id = Integer.parseInt(v.getTag().toString());
adb.setNegativeButton("TIDAK", null);
adb.setPositiveButton("YA",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// MyDataObject.remove(positionToRemove);
DatabaseHandler dBHandler = new DatabaseHandler(
activity.getApplicationContext());
dBHandler.Delete_Contact(user_id);
Main_Screen.this.onResume();
}
});
adb.show();
}
});
return row;
}
class UserHolder {
TextView name;
TextView email;
TextView number;
TextView kelamn;
TextView agam;
Button edit;
Button delete;
}
}
}
DatabaseHandler.java
package com.example.dbs;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "MISRAL_DB";
// Contacts table name
private static final String TABLE_BIODATA = "biodata";
// Contacts Table Columns names
private static final String ID = "id";
private static final String NPM = "npm";
private static final String NAMA = "nama";
private static final String ALAMAT = "alamat";
private static final String KELAMIN = "kelamin";
private static final String AGAMA = "agama";
private final ArrayList<Contact> contact_list = new ArrayList<Contact>();
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_BIODATA + "("
+ ID + " INTEGER PRIMARY KEY," + NPM + " TEXT,"
+ NAMA + " TEXT," + ALAMAT + " TEXT," + KELAMIN + " TEXT," + AGAMA + " TEXT" + " )";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BIODATA);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
public void Add_Contact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NPM, contact.getNpm()); // Contact Name
values.put(NAMA, contact.getNama()); // Contact Phone
values.put(ALAMAT, contact.getAlamat()); // Contact Email
values.put(KELAMIN, contact.getKelamin());
values.put(AGAMA, contact.getAgama());
// Inserting Row
db.insert(TABLE_BIODATA, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact Get_Contact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_BIODATA, new String[] { ID,
NPM, NAMA, ALAMAT, KELAMIN, AGAMA }, ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
// return contact
cursor.close();
db.close();
return contact;
}
// Getting All Contacts
public ArrayList<Contact> Get_Contacts() {
try {
contact_list.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_BIODATA;
SQLiteDatabase db = this.getWritableDatabase();
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.setNpm(cursor.getString(1));
contact.setNama(cursor.getString(2));
contact.setAlamat(cursor.getString(3));
contact.setKelamin(cursor.getString(4));
contact.setAgama(cursor.getString(5));
// Adding contact to list
contact_list.add(contact);
} while (cursor.moveToNext());
}
// return contact list
cursor.close();
db.close();
return contact_list;
} catch (Exception e) {
// TODO: handle exception
Log.e("all_contact", "" + e);
}
return contact_list;
}
// Updating single contact
public int Update_Contact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NPM, contact.getNpm());
values.put(NAMA, contact.getNama());
values.put(ALAMAT, contact.getAlamat());
values.put(KELAMIN, contact.getKelamin());
values.put(AGAMA, contact.getAgama());
// updating row
return db.update(TABLE_BIODATA, values, ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void Delete_Contact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_BIODATA, ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
// Getting contacts Count
public int Get_Total_Contacts() {
String countQuery = "SELECT * FROM " + TABLE_BIODATA;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
Contact.java
package com.example.dbs;
public class Contact {
// private variables
public int _id;
public String _npm;
public String _nama;
public String _alamat;
public String _kelamin;
public String _agama;
public Contact() {
}
// constructor
public Contact(int id, String npm, String _nama, String _alamat, String _kelamin, String _agama) {
this._id = id;
this._npm = npm;
this._nama = _nama;
this._alamat = _alamat;
this._kelamin = _kelamin;
this._agama = _agama;
}
// constructor
public Contact(String npm, String _nama, String _alamat, String _kelamin, String _agama) {
this._npm = npm;
this._nama = _nama;
this._alamat = _alamat;
this._kelamin = _kelamin;
this._agama = _agama;
}
// getting ID
public int getID() {
return this._id;
}
// setting id
public void setID(int id) {
this._id = id;
}
// getting name
public String getNpm() {
return this._npm;
}
// setting name
public void setNpm(String npm) {
this._npm = npm;
}
// getting phone number
public String getNama() {
return this._nama;
}
// setting phone number
public void setNama(String _nama) {
this._nama = _nama;
}
// getting email
public String getAlamat() {
return this._alamat;
}
// setting email
public void setAlamat(String alamat) {
this._alamat = alamat;
}
public String getKelamin() {
return this._kelamin;
}
// setting email
public void setKelamin(String _kelamin) {
this._kelamin = _kelamin;
}
public String getAgama() {
return this._agama;
}
// setting email
public void setAgama(String _agama) {
this._agama = _agama;
}
}
Add_Update_User.java
package com.example.dbs;
import com.example.dbs.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class Add_Update_User extends Activity {
EditText add_npm, add_nama, add_alamat;
Button add_save_btn, add_view_all, resett;
Spinner addagama;
RadioGroup rg;
RadioButton kelamin,lk,pr;
LinearLayout add_view, update_view;
String valid_npm = null, valid_alamat = null, valid_nama = null,
Toast_msg = null, valid_user_id = "", v_kelamin, v_agama;
int USER_ID;
DatabaseHandler dbHandler = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_update_screen);
add_npm = (EditText) findViewById(R.id.nim);
add_nama = (EditText) findViewById(R.id.nama);
add_alamat = (EditText) findViewById(R.id.alamat);
add_save_btn = (Button) findViewById(R.id.simpann);
resett = (Button) findViewById(R.id.button1);
add_view_all = (Button) findViewById(R.id.tampill);
rg=(RadioGroup)findViewById(R.id.sexsex);
int selectid = rg.getCheckedRadioButtonId();
kelamin = (RadioButton)findViewById(selectid);
addagama=(Spinner)findViewById(R.id.agama);
lk =(RadioButton)findViewById(R.id.radio0);
pr =(RadioButton)findViewById(R.id.radio1);
// set visibility of view as per calling activity
String called_from = getIntent().getStringExtra("called");
if (called_from.equalsIgnoreCase("Simpan")) {
Bundle b = getIntent().getExtras();
String sim = b.getString("called");
add_save_btn.setText(sim);
} else if (called_from.equalsIgnoreCase("Update")) {
Bundle b = getIntent().getExtras();
String updt = b.getString("called");
add_save_btn.setText(updt);
USER_ID = Integer.parseInt(getIntent().getStringExtra("USER_ID"));
Contact c = dbHandler.Get_Contact(USER_ID);
add_npm.setText(c.getNpm());
add_npm.setText("");
add_nama.setText(c.getNama());
add_alamat.setText(c.getAlamat());
String kel= c.getKelamin();
String agam = c.getAgama();
add_npm.setText(c.getNpm());
if (kel.toString().equals("Laki-laki")){
lk.setChecked(true);
}else if (kel.toString().equals("Perempuan")){
pr.setChecked(true);
}
if (agam.toString().equals("Islam")){
addagama.setSelection(0);
} else if (agam.toString().equals("Hindu")){
addagama.setSelection(1);
} else if (agam.toString().equals("Budha")){
addagama.setSelection(2);
} else if (agam.toString().equals("Protestan")){
addagama.setSelection(3);
} else if (agam.toString().equals("Katholik")){
addagama.setSelection(4);
}
//dbHandler.close();
}
add_npm.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// min lenth 10 and max lenth 12 (2 extra for - as per phone
// matcher format)
Is_Valid_Sign_Number_Validation(13, 13, add_npm);
}
});
resett.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Reset_Text();
add_save_btn.setText("Simpan");
}
});
add_save_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// check the value state is null or not
int selectid = rg.getCheckedRadioButtonId();
kelamin = (RadioButton)findViewById(selectid);
if (add_save_btn.getText().toString().equals("Update")){
upDate();
add_save_btn.setText("Simpan");
} else if (add_save_btn.getText().toString().equals("Simpan")) {
if (!add_nama.getText().toString().equals("") && !add_alamat.getText().toString().equals("") && !add_npm.getText().toString().equals("") && valid_npm !=null){
valid_alamat=add_alamat.getText().toString();
valid_nama=add_nama.getText().toString();
v_kelamin = kelamin.getText().toString();
v_agama = String.valueOf(addagama.getSelectedItem());
dbHandler.Add_Contact(new Contact(valid_npm, valid_nama, valid_alamat, v_kelamin, v_agama ));
Toast_msg = "Data telah disimpan";
Show_Toast(Toast_msg);
Reset_Text();
} else
{
Show_Toast("Data belum lengkap");
}
} else {
Show_Toast("Maaf,field tidak diketahui.");
add_save_btn.setText("Simpan");
}
}
});
add_view_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent view_user = new Intent(Add_Update_User.this,
Main_Screen.class);
view_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(view_user);
finish();
}
});
}
private void upDate() {
// TODO Auto-generated method stub
int selectid = rg.getCheckedRadioButtonId();
kelamin = (RadioButton)findViewById(selectid);
valid_alamat=add_alamat.getText().toString();
valid_nama=add_nama.getText().toString();
valid_npm=add_npm.getText().toString();
v_kelamin = kelamin.getText().toString();
v_agama = String.valueOf(addagama.getSelectedItem());
dbHandler.Update_Contact(new Contact(USER_ID, valid_npm, valid_nama, valid_alamat, v_kelamin, v_agama));
dbHandler.close();
Toast_msg = "Data telah di update";
Show_Toast(Toast_msg);
Reset_Text();
}
public void Set_Add_Update_Screen() {
}
public void Is_Valid_Sign_Number_Validation(int MinLen, int MaxLen,
EditText edt) throws NumberFormatException {
if (edt.getText().toString().length() <= 0) {
edt.setError("Format NPM 13 digit");
valid_npm = null;
} else if (edt.getText().toString().length() < MinLen) {
edt.setError("Maksimal " + MinLen + " digit");
valid_npm = null;
} else if (edt.getText().toString().length() > MaxLen) {
edt.setError("Maksimal " + MinLen + " digit");
valid_npm = null;
} else {
valid_npm = edt.getText().toString();
}
} // END OF Edittext validation
public void Is_Valid_Email(EditText edt) {
if (edt.getText().toString() == null) {
edt.setError("Invalid Email Address");
valid_alamat = null;
} else if (isEmailValid(edt.getText().toString()) == false) {
edt.setError("Invalid Email Address");
valid_alamat = null;
} else {
valid_alamat = edt.getText().toString();
}
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
} // end of email matcher
public void Show_Toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
public void Reset_Text() {
add_npm.getText().clear();
add_nama.getText().clear();
add_alamat.getText().clear();
lk.setChecked(true);
addagama.setSelection(0);
}
}
No comments:
Post a Comment