12.44
0
Kali ini saya akan membuat aplikasi sederhana berbasis android dengan menggunakan sqllite. Aplikasi ini digunakan untuk menyimpan nama, no telepon dan email di kontak android Anda.
Tampilan Inputan Data
Tampilan Edit Hapus Data

Hasil Eksekusi Data





DatabaseHandler.java

package com.anjar.uas003111121049;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
protected static final String DATABASE_NAME = "StudentDatabase";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE students " +
"( id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"firstname TEXT, " +
"handphone TEXT, " +
"alamat TEXT, " +
"email TEXT ) ";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS students";
db.execSQL(sql);
onCreate(db);
}
}
MainActivity.Java

package com.anjar.uas003111121049;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/*
 * Steps:
 *
 */
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonCreateLocation = (Button) findViewById(R.id.buttonCreateStudent);
buttonCreateLocation.setOnClickListener(new OnClickListenerCreateStudent());
countRecords();
readRecords();
}
public void countRecords() {
int recordCount = new TableControllerStudent(this).count();
TextView textViewRecordCount = (TextView) findViewById(R.id.textViewRecordCount);
textViewRecordCount.setText(recordCount + " records found.");
}
public void readRecords() {
LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
linearLayoutRecords.removeAllViews();
List students = new TableControllerStudent(this).read();
if (students.size() > 0) {
for (ObjectStudent obj : students) {
int id = obj.id;
String studentFirstname = obj.firstname;
String studentEmail = obj.email;
String studentHandphone = obj.handphone;
String studentAlamat = obj.alamat;
String textViewContents = studentFirstname + " - " + studentEmail + " - " + studentHandphone + " - " + studentAlamat;
TextView textViewLocationItem = new TextView(this);
textViewLocationItem.setPadding(0, 10, 0, 10);
textViewLocationItem.setText(textViewContents);
textViewLocationItem.setTag(Integer.toString(id));
textViewLocationItem.setOnLongClickListener(new OnLongClickListenerStudentRecord());
linearLayoutRecords.addView(textViewLocationItem);
}
}
else {
TextView locationItem = new TextView(this);
locationItem.setPadding(8, 8, 8, 8);
locationItem.setText("No records yet.");
linearLayoutRecords.addView(locationItem);
}
}
@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;
}
}
 ObjectStudent.java

package com.anjar.uas003111121049;
public class ObjectStudent {
int id;
String firstname;
String email;
String handphone;
String alamat;
public ObjectStudent(){
}
}
OnClickListenerCreateStudent.java

package com.anjar.uas003111121049;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class OnClickListenerCreateStudent implements OnClickListener {
@Override
public void onClick(View view) {
final Context context = view.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);
final EditText editTextStudentFirstname = (EditText) formElementsView.findViewById(R.id.editTextStudentFirstname);
final EditText editTextStudentEmail = (EditText) formElementsView.findViewById(R.id.editTextStudentEmail);
final EditText editTextStudentHandphone = (EditText) formElementsView.findViewById(R.id.editTextStudentHandphone);
final EditText editTextStudentAlamat = (EditText) formElementsView.findViewById(R.id.editTextStudentAlamat);
new AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("Create Student")
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String studentFirstname = editTextStudentFirstname.getText().toString();
String studentEmail = editTextStudentEmail.getText().toString();
String studentHanphone = editTextStudentHandphone.getText().toString();
String studentAlamat = editTextStudentAlamat.getText().toString();
ObjectStudent objectStudent = new ObjectStudent();
objectStudent.firstname = studentFirstname;
objectStudent.email = studentEmail;
objectStudent.handphone = studentHanphone;
objectStudent.alamat = studentAlamat;
boolean createSuccessful = new TableControllerStudent(context).create(objectStudent);
if(createSuccessful){
Toast.makeText(context, "Student information was saved.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Unable to save student information.", Toast.LENGTH_SHORT).show();
}
((MainActivity) context).countRecords();
((MainActivity) context).readRecords();
dialog.cancel();
}
}).show();
}
}
OnLongClickListenerStudentRecord.java

package com.anjar.uas003111121049;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class OnLongClickListenerStudentRecord implements OnLongClickListener {
Context context;
String id;
@Override
public boolean onLongClick(View view) {
context = view.getContext();
id = view.getTag().toString();
final CharSequence[] items = { "Edit", "Delete" };
new AlertDialog.Builder(context).setTitle("Student Record")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
editRecord(Integer.parseInt(id));
}
else if (item == 1) {
boolean deleteSuccessful = new TableControllerStudent(context).delete(id);
if (deleteSuccessful){
Toast.makeText(context, "Student record was deleted.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Unable to delete student record.", Toast.LENGTH_SHORT).show();
}
((MainActivity) context).countRecords();
((MainActivity) context).readRecords();
}
dialog.dismiss();
}
}).show();
return false;
}
public void editRecord(final int studentId) {
final TableControllerStudent tableControllerStudent = new TableControllerStudent(context);
ObjectStudent objectStudent = tableControllerStudent.readSingleRecord(studentId);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);
final EditText editTextStudentFirstname = (EditText) formElementsView.findViewById(R.id.editTextStudentFirstname);
final EditText editTextStudentEmail = (EditText) formElementsView.findViewById(R.id.editTextStudentEmail);
final EditText editTextStudentHandphone = (EditText) formElementsView.findViewById(R.id.editTextStudentHandphone);
final EditText editTextStudentAlamat = (EditText) formElementsView.findViewById(R.id.editTextStudentAlamat);
editTextStudentFirstname.setText(objectStudent.firstname);
editTextStudentEmail.setText(objectStudent.email);
new AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("Edit Record")
.setPositiveButton("Save Changes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ObjectStudent objectStudent = new ObjectStudent();
objectStudent.id = studentId;
objectStudent.firstname = editTextStudentFirstname.getText().toString();
objectStudent.email = editTextStudentEmail.getText().toString();
objectStudent.handphone = editTextStudentHandphone.getText().toString();
objectStudent.alamat = editTextStudentAlamat.getText().toString();
boolean updateSuccessful = tableControllerStudent.update(objectStudent);
if(updateSuccessful){
Toast.makeText(context, "Student record was updated.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Unable to update student record.", Toast.LENGTH_SHORT).show();
}
((MainActivity) context).countRecords();
((MainActivity) context).readRecords();
dialog.cancel();
}
}).show();
}
}
TableControllerStudent.java

package com.anjar.uas003111121049;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class TableControllerStudent extends DatabaseHandler {
public TableControllerStudent(Context context) {
super(context);
}
public boolean create(ObjectStudent objectStudent) {
ContentValues values = new ContentValues();
values.put("firstname", objectStudent.firstname);
values.put("email", objectStudent.email);
values.put("handphone", objectStudent.handphone);
values.put("alamat", objectStudent.alamat);
SQLiteDatabase db = this.getWritableDatabase();
boolean createSuccessful = db.insert("students", null, values) > 0;
db.close();
return createSuccessful;
}
public List read() {
List recordsList = new ArrayList();
String sql = "SELECT * FROM students ORDER BY id DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, null);
if (cursor.moveToFirst()) {
do {
int id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("id")));
String studentFirstname = cursor.getString(cursor.getColumnIndex("firstname"));
String studentEmail = cursor.getString(cursor.getColumnIndex("email"));
String studentHandphone = cursor.getString(cursor.getColumnIndex("handphone"));
String studentAlamat = cursor.getString(cursor.getColumnIndex("alamat"));
ObjectStudent objectStudent = new ObjectStudent();
objectStudent.id = id;
objectStudent.firstname = studentFirstname;
objectStudent.email = studentEmail;
objectStudent.handphone = studentHandphone;
objectStudent.alamat = studentAlamat;
recordsList.add(objectStudent);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return recordsList;
}
public ObjectStudent readSingleRecord(int studentId) {
ObjectStudent objectStudent = null;
String sql = "SELECT * FROM students WHERE id = " + studentId;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, null);
if (cursor.moveToFirst()) {
int id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("id")));
String firstname = cursor.getString(cursor.getColumnIndex("firstname"));
String email = cursor.getString(cursor.getColumnIndex("email"));
String handphone = cursor.getString(cursor.getColumnIndex("handphone"));
String alamat = cursor.getString(cursor.getColumnIndex("alamat"));
objectStudent = new ObjectStudent();
objectStudent.id = id;
objectStudent.firstname = firstname;
objectStudent.email = email;
objectStudent.handphone = handphone;
objectStudent.alamat = alamat;
}
cursor.close();
db.close();
return objectStudent;
}
public boolean update(ObjectStudent objectStudent) {
ContentValues values = new ContentValues();
values.put("firstname", objectStudent.firstname);
values.put("email", objectStudent.email);
values.put("handphone", objectStudent.handphone);
values.put("alamat", objectStudent.alamat);
String where = "id = ?";
String[] whereArgs = { Integer.toString(objectStudent.id) };
SQLiteDatabase db = this.getWritableDatabase();
boolean updateSuccessful = db.update("students", values, where, whereArgs) > 0;
db.close();
return updateSuccessful;
}
public boolean delete(String id) {
SQLiteDatabase db = this.getWritableDatabase();
boolean deleteSuccessful = db.delete("students", "id = " + id, null) > 0;
db.close();
return deleteSuccessful;
}
public int count() {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "SELECT * FROM students";
int recordCount = db.rawQuery(sql, null).getCount();
db.close();
return recordCount;
}
}


0 komentar:

Posting Komentar