Professional Documents
Culture Documents
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
ArrayList<Customer> currentData;
SQLiteDatabase database;
CustomerListAdapter adapter;
ListView list;
CustomerSQLHelper helper;
Customer cust;
TextView txtTitle;
Utils util;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
database = helper.getWritableDatabase();
currentData = util.loadData();
adapter = new CustomerListAdapter(this, currentData);
list.setAdapter(adapter);
list.setEmptyView(findViewById(R.id.list_empty));
list.setOnItemClickListener(new OnItemClickListener() {
adapter.toggleDataCompleteAtPosition(position);
});
list.setOnItemLongClickListener(new OnItemLongClickListener() {
Customer c = adapter.getItem(position);
util.onShowData(c, DatabaseApps.this);
return false;
});
// ----------------------------------------------
@Override
super.onResume();
adapter.forceReload();
// -----------------------------------------------
btnAdd.setOnClickListener(new OnClickListener() {
});
btnUpdate.setOnClickListener(new OnClickListener() {
Customer c = adapter.getCheckedCustomer();
if (!c.getName().equals(""))
onCreateWidgetData(2, c);
else {
Toast.makeText(DatabaseApps.this, "Harus centang satu",
Toast.LENGTH_LONG).show();
});
btnDelete.setOnClickListener(new OnClickListener() {
Customer c = adapter.getCheckedCustomer();
onDeleteData(c.getId());
});
btnExit.setOnClickListener(new OnClickListener() {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
});
case 1:
widgetAdd();
break;
case 2:
widgetUpdate(getCust);
break;
setContentView(R.layout.inputdata);
txtTitle.setText("Add Data");
btnSubmit.setOnClickListener(new OnClickListener() {
|| dtAddress.getText().length()<1
|| dtPhone.getText().length()<1) {
Toast.LENGTH_SHORT).show();
else {
cust.setName(dtName.getText().toString());
cust.setAddress(dtAddress.getText().toString());
cust.setPhone(dtPhone.getText().toString());
cust.setComplete(false);
util.onSaveData(cust);
onCancel();
});
btnCancel.setOnClickListener(new OnClickListener() {
onCancel();
});
}
public void widgetUpdate(final Customer getCust) {
setContentView(R.layout.inputdata);
txtTitle.setText("Update Data");
dtName.setText(getCust.getName());
dtAddress.setText(getCust.getAddress());
dtPhone.setText(getCust.getPhone());
btnSubmit.setOnClickListener(new OnClickListener() {
if (dtName.getText().length()<1
|| dtAddress.getText().length()<1
|| dtPhone.getText().length()<1) {
else {
getCust.setName(dtName.getText().toString());
getCust.setAddress(dtAddress.getText().toString());
getCust.setPhone(dtPhone.getText().toString());
util.onUpdateData(getCust); onCancel();
});
btnCancel.setOnClickListener(new OnClickListener() {
onCancel();
});
// deleteData(ids);
deleteData(new Long[]{id});
currentData = util.loadData();
list.setAdapter(adapter);
@SuppressWarnings("static-access")
idList.append(ids[i]);
if (i < ids.length -1 ) {
idList.append(",");
DatabaseApps.class);
startActivity(newIntent);
finish();
}
// -----------------------------------------------
package com.wilis.databaseapp;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
ArrayList<Customer> cust;
Context context;
super();
this.cust = custs;
this.context = context;
}
@Override
return cust.size();
@Override
@Override
return position;
@Override
ViewHolder holder;
View vi = convertView;
if (null == convertView) {
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = infalInflater.inflate(R.layout.listitem, null);
holder.nameView = (CheckedTextView)vi.findViewById(R.id.txt_name);
vi.setTag(holder);
else
holder.nameView.setText(txtName);
holder.nameView.setChecked(check);
holder.idView.setText(txtId);
return vi;
}
public void forceReload() {
notifyDataSetChanged();
cust.toggleComplete();
notifyDataSetChanged();
if (dtCust.isComplete()) {
completedTasks.add(dtCust);
completedIds.add(dtCust.getId());
cust.removeAll(completedTasks);
notifyDataSetChanged();
if (dtCust.isComplete()) {
return newCust;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
CustomerSQLHelper helper;
SQLiteDatabase database;
public Utils(Context ctx) {
database = helper.getWritableDatabase();
@SuppressWarnings("static-access")
helper.TASK_ADDRESS,
helper.TASK_PHONE, helper.TASK_COMPLETE},
dataCursor.moveToFirst();
Customer t;
if ( !dataCursor.isAfterLast() ) {
do {
t = new Customer();
t.setId(id);
t.setName(name);
t.setAddress(addr);
t.setPhone(phon);
t.setComplete(complete);
currentData.add(t);
} while(dataCursor.moveToNext());
/*
while (dataCursor.moveToNext()) {
*/
dataCursor.close();
return currentData;
}
@SuppressWarnings("static-access")
values.put(helper.TASK_NAME, getCust.getName());
values.put(helper.TASK_ADDRESS, getCust.getAddress());
values.put(helper.TASK_PHONE, getCust.getPhone());
values.put(helper.TASK_COMPLETE, Boolean.toString(false));
@SuppressWarnings("static-access")
values.put(helper.TASK_NAME, getCust.getName());
values.put(helper.TASK_ADDRESS, getCust.getAddress());
values.put(helper.TASK_PHONE, getCust.getPhone());
values.put(helper.TASK_COMPLETE, Boolean.toString(getCust.isComplete()));
long id = getCust.getId();
AlertDialog alert;
.setTitle("Display Data")
+ "ID: "+thisCust.getId()+"\n"
+ "Name: "+thisCust.getName()+"\n"
+ "Adress: "+thisCust.getAddress()+"\n"
+ "Phone: "+thisCust.getPhone()+"\n")
alert.cancel();
}).create();
alert.show();
<EditText
android:id="@+id/data_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Isikan Nama Anda" />
<EditText
android:id="@+id/data_address"
android:layout_width="fill_parent"
android:layout_height="100px"
android:gravity="top"
android:hint="Alamat Anda"
android:singleLine="false" />
<EditText
android:id="@+id/data_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Nomor Telpon"
android:phoneNumber="true" />
<Button
android:id="@+id/submit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Kirim" />
<Button
android:id="@+id/cancel_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Kembali" />
</LinearLayout>
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="35px"
android:text="Tambah" />
<Button
android:id="@+id/update_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/add_button"
android:text="Edit" />
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/update_button"
android:text="Hapus" />
<Button
android:id="@+id/exit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/delete_button"
android:text="Keluar" />
</RelativeLayout>
<CheckedTextView android:id="@+id/txt_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Name Customer"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
<TextView android:id="@+id/txt_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:visibility="gone" />
</LinearLayout>
Hasil Running :