Pertama kita akan membuka aplikasi dengan mengklik ContactApps
Tampilan pertama aplikasi
Tampilan Inputan Data
Setelah diinput
Hapus kontak
Edit Kontak
View Kontak :v
MainActivity.java
package id.pratama.contactapps;import id.pratama.contactapps.adapter.ContactAdapter;
import id.pratama.contactapps.database.ContactDB;
import id.pratama.contactapps.entity.Contact;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements
OnItemClickListener
{
private ListView lv;
private ContactDB db;
private ContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = ContactDB.getInstance(this);
lv = (ListView) findViewById(R.id.listview_contact);
lv.setEmptyView(findViewById(R.id.empty));
if (db.isContactHasData())
{
adapter = new ContactAdapter(this, db.getAllContact());
lv.setAdapter(adapter);
}
lv.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_add:
Toast.makeText(this, "add", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, AddContactActivity.class));
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
Bundle b = new Bundle();
b.putInt("id", ((Contact) adapter.getItem(position)).getId());
b.putString("nama", ((Contact) adapter.getItem(position)).getNama());
b.putString("phone", ((Contact) adapter.getItem(position)).getNumber());
b.putString("email", ((Contact) adapter.getItem(position)).getEmail());
Intent i = new Intent(this, DetailContactActivity.class);
i.putExtras(b);
startActivity(i);
finish();
}
}
AddContactActivity.java
package id.pratama.contactapps;
import id.pratama.contactapps.database.ContactDB;
import id.pratama.contactapps.entity.Contact;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddContactActivity extends ActionBarActivity implements
OnClickListener
{
private EditText nama, email, phone;
private Button cancel, done;
private ContactDB db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
db = ContactDB.getInstance(this);
nama = (EditText) findViewById(R.id.input_name);
email = (EditText) findViewById(R.id.input_email);
phone = (EditText) findViewById(R.id.input_phone);
cancel = (Button) findViewById(R.id.btn_cancel);
done = (Button) findViewById(R.id.btn_done);
cancel.setOnClickListener(this);
done.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v == done)
{
String txtemail = "null";
if (!TextUtils.isEmpty(nama.getText().toString())
&& !TextUtils.isEmpty(phone.getText().toString()))
{
if (!TextUtils.isEmpty(email.getText().toString()))
txtemail = email.getText().toString();
db.addContact(new Contact(nama.getText().toString(), phone
.getText().toString(), txtemail));
startActivity(new Intent(this, MainActivity.class));
finish();
} else
{
Toast.makeText(this, "Lengkapi Data", Toast.LENGTH_SHORT)
.show();
}
} else if (v == cancel)
{
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
@Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
finish();
startActivity(new Intent(this, MainActivity.class));
}
}
EditActivity.java
package id.pratama.contactapps;
import id.pratama.contactapps.database.ContactDB;
import id.pratama.contactapps.entity.Contact;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditActivity extends ActionBarActivity implements OnClickListener
{
private EditText nama, email, phone;
private Button cancel, done;
private ContactDB db;
private int id;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
getSupportActionBar().setTitle("Edit Contact");
db = ContactDB.getInstance(this);
nama = (EditText) findViewById(R.id.edit_name);
email = (EditText) findViewById(R.id.edit_email);
phone = (EditText) findViewById(R.id.edit_phone);
cancel = (Button) findViewById(R.id.btn_cancel);
done = (Button) findViewById(R.id.btn_done);
Bundle b = getIntent().getExtras();
if (b != null)
{
id = b.getInt("id");
nama.setText(b.getString("name"));
phone.setText(b.getString("phone"));
email.setText(b.getString("email"));
}
cancel.setOnClickListener(this);
done.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v == done)
{
db.editContact(new Contact(id, nama.getText().toString(), phone
.getText().toString(), email.getText().toString()));
startActivity(new Intent(this, MainActivity.class));
finish();
} else if (v == cancel)
{
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
@Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
EditContactActivity.java
package id.pratama.contactapps;
import id.pratama.contactapps.database.ContactDB;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class DetailContactActivity extends ActionBarActivity implements
OnClickListener
{
private TextView phone, email;
private int id;
private ImageView call, btnEmail;
private ContactDB db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_contact);
db = ContactDB.getInstance(this);
phone = (TextView) findViewById(R.id.txt_phone_number);
email = (TextView) findViewById(R.id.txt_email);
call = (ImageView) findViewById(R.id.btn_call);
btnEmail = (ImageView) findViewById(R.id.btn_email);
Bundle b = getIntent().getExtras();
if (b != null)
{
id = b.getInt("id");
phone.setText(b.getString("phone"));
getSupportActionBar().setTitle(b.getString("nama"));
if (!b.getString("email").equals("null"))
{
email.setText(b.getString("email"));
} else
{
email.setText("");
}
}
call.setOnClickListener(this);
btnEmail.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.detail_contact, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_edit:
Bundle b = new Bundle();
b.putInt("id", id);
b.putString("name", getSupportActionBar().getTitle().toString());
b.putString("email", email.getText().toString());
b.putString("phone", phone.getText().toString());
Intent i = new Intent(this, EditActivity.class);
i.putExtras(b);
startActivity(i);
finish();
return true;
case R.id.action_delete:
showSettingsAlert();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onClick(View v)
{
if (v == btnEmail)
{
if (!email.getText().toString().equals(""))
{
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto", email.getText().toString(),
null));
startActivity(Intent
.createChooser(emailIntent, "Send email..."));
}
} else if (v == call)
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phone.getText().toString()));
startActivity(callIntent);
}
}
private void showSettingsAlert()
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Delete");
alert.setMessage("Contact akan dihapus");
alert.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
db.deleteContact(id);
startActivity(new Intent(DetailContactActivity.this,
MainActivity.class));
finish();
}
});
alert.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
alert.show();
}
@Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(DetailContactActivity.this, MainActivity.class));
finish();
}
}
ContactDB.java
package id.pratama.contactapps.database;
import java.util.ArrayList;
import java.util.List;
import id.pratama.contactapps.entity.Contact;
import id.pratama.contactapps.util.Utils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ContactDB extends SQLiteOpenHelper
{
private static final String DB_NAME = "db_contact";
private static final int DB_VERSION = 1;
private static ContactDB dbInstance;
private static SQLiteDatabase db;
private static final String TB_CONTACT = "tb_contact";
interface COLUMN
{
String id = "_id";
String nama = "nama";
String nomor = "nomor";
String email = "email";
}
private static final String SQL_CREATE_CONTACT = "CREATE TABLE "
+ TB_CONTACT
+ "( "
+ COLUMN.id
+ " integer primary key autoincrement not null, "
+ COLUMN.nama
+ " text, "
+ COLUMN.email
+ " text, "
+ COLUMN.nomor
+ " text)";
private ContactDB(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
public static ContactDB getInstance(Context context)
{
if (dbInstance == null)
{
dbInstance = new ContactDB(context);
db = dbInstance.getWritableDatabase();
}
return dbInstance;
}
@Override
public synchronized void close()
{
super.close();
if (dbInstance != null)
dbInstance.close();
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
Utils.TRACE("ContactDB", "" + SQL_CREATE_CONTACT);
db.execSQL(SQL_CREATE_CONTACT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXIST " + TB_CONTACT);
onCreate(db);
}
public List<Contact> getAllContact()
{
List<Contact> listContacts = new ArrayList<Contact>();
Cursor cursor = db.query(TB_CONTACT, new String[] { COLUMN.id,
COLUMN.nama, COLUMN.email, COLUMN.nomor }, null, null, null,
null, COLUMN.nama);
cursor.moveToFirst();
do
{
listContacts
.add(new Contact(cursor.getInt(cursor
.getColumnIndexOrThrow(COLUMN.id)), cursor
.getString(cursor
.getColumnIndexOrThrow(COLUMN.nama)),
cursor.getString(cursor
.getColumnIndexOrThrow(COLUMN.nomor)),
cursor.getString(cursor
.getColumnIndexOrThrow(COLUMN.email))));
} while (cursor.moveToNext());
return listContacts;
}
public boolean isContactHasData()
{
Cursor cursor = db.query(TB_CONTACT, new String[] { COLUMN.id }, null,
null, null, null, null);
return (cursor.getCount() > 0) ? true : false;
}
public boolean addContact(Contact contact)
{
ContentValues values = new ContentValues();
values.put(COLUMN.nama, contact.getNama());
values.put(COLUMN.email, contact.getEmail());
values.put(COLUMN.nomor, contact.getNumber());
return ((db.insert(TB_CONTACT, null, values)) != -1) ? true : false;
}
public boolean editContact(Contact contact)
{
ContentValues values = new ContentValues();
values.put(COLUMN.nama, contact.getNama());
values.put(COLUMN.email, contact.getEmail());
values.put(COLUMN.nomor, contact.getNumber());
return ((db.update(TB_CONTACT, values, COLUMN.id + "=?",
new String[] { String.valueOf(contact.getId()) })) == 1) ? true
: false;
}
public void deleteContact(int id)
{
db.delete(TB_CONTACT, COLUMN.id + "=?",
new String[] { String.valueOf(id) });
}
}