Tampilan Aplikasi
package com.inputreptil.screen;
public class MainActivity extends Activity
implements
OnClickListener
{
btn_add
= (Button) findViewById(R.id.btn_add);
btn_view
= (Button) findViewById(R.id.btn_view);
btn_add.setOnClickListener(this);
btn_view.setOnClickListener(this);
public
void onClick(View v) {
switch
(v.getId()) {
case
R.id.btn_add:
Intent
addintent = new Intent(MainActivity.this,
AddRecord.class);
startActivity(addintent);
break;
case
R.id.btn_view:
Intent
viewintent = new Intent(MainActivity.this,
ViewRecord.class);
startActivity(viewintent);
break;
default:
break;
package com.inputreptil.screen;
import com.inputreptil.db.DatabaseHelper;
public class AddRecord extends Activity implements
OnClickListener {
Kode
deklarasi tampilan form untuk menginput data hewan
txtpnama
= (EditText) findViewById(R.id.txtpnama);
txtharga
= (EditText) findViewById(R.id.txtharga);
btn_pencarian
= (Button) findViewById(R.id.btn_pencarian);
txtpid
= (EditText) findViewById(R.id.txtpid);
btn_addrecord.setOnClickListener(this);
Proses
input data hewan berdasarkan id, nama hewan, dan harga
db
= new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
pm
= new ProductModel();
pm.idno
= (txtpid.getText().toString());
pm.productnama
= txtpname.getText().toString();
pm.productharga
= txtpprice.getText().toString();
Log.i("idno,productnama,productharga",
"" + pm.idno + ""
+
pm.productnama + "" + pm.productharga);
db.addProduct(pm);
Toast.makeText(AddRecord.this,
"Record Added successfully.",
Toast.LENGTH_LONG).show();
finish();
package com.inputhewan.screen;
import com.inputhewan.db.DatabaseHelper;
public class AddUpdateValues extends Activity
implements OnClickListener {
Menggunakan
intent
i
= getIntent();
txtpnama
= (EditText) findViewById(R.id.txt_udatepnama);
txtharga
= (EditText) findViewById(R.id.txt_udateharga);
txtpnama.setText(i.getExtras().getString("nama"));
txtharga.setText(i.getExtras().getString("harga"));
btn_updaterecord
= (Button) findViewById(R.id.btn_updaterecord);
btn_updaterecord.setOnClickListener(this);
Proses
kerja edit data hewan
db
= new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
pm
= new ProductModel();
pm.productnama = txtnama.getText().toString();
pm.productharga
= txtharga.getText().toString();
pm.idno
= i.getExtras().getString("id");
Log.i(">>>>>productid<<<<<",
"" + i.getExtras().getString("id"));
db.updateHewan(pm);
Toast.makeText(AddUpdateValues.this,
"Record
Update successfully.", Toast.LENGTH_LONG)
.show();
db.close();
super.onResume();
finish();
Database
tabel
package com.inputhewan.db;
import com.inputhewan.screen.HewanJenis;
public class DatabaseHelper extends SQLiteOpenHelper
{
public
void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE
TABLE if not exists producttable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+
"hewanidno"
+
" TEXT ,"
+
"namahewan"
+
" TEXT,"
+ "hargahewan" + "
TEXT)");
public
void addHewan(HewanJenis productitem) {
SQLiteDatabase
db = this.getWritableDatabase();
ContentValues
contentValues = new ContentValues();
contentValues.put("hewanidno",
hewanitem.idno);
contentValues.put("namahewan",
hewanitem.namahewan);
contentValues.put("hargahewan",
hewanitem.hargahewan);
db.insert("producttable",
null, contentValues);
db.close();
public
void updateHewan(JenisHewan productList) {
SQLiteDatabase
db = this.getWritableDatabase();
ContentValues
contentValues = new ContentValues();
contentValues.put("namahewan",
hewanList.namahewan);
contentValues.put("productprice",
productList.productprice);
db.update("hewantable",
contentValues, "productidno="
+
productList.idno, null);
db.close();
public
void emptyProduct() {
try
{
SQLiteDatabase
db = this.getWritableDatabase();
db.execSQL("delete
from hewantable");
db.close();
}
catch (Exception e) {
e.printStackTrace();
public
void removeProduct(String productid, String pnama, String hargahewan) {
try
{
String[]
args = { hewanid };
getWritableDatabase().delete("hewantable",
"hewanidno=?", args);
}
catch (Exception e) {
e.printStackTrace();
public
ArrayList<JenisHewan> getHewan() {
cartList.clear();
SQLiteDatabase
db = this.getWritableDatabase();
Cursor
cursor = db.rawQuery("select * from hewantable", null);
if
(cursor.getCount() != 0) {
if
(cursor.moveToFirst()) {
do
{
JenisHewan
item = new JenisHewan();
item.idno
= cursor.getString(cursor
.getColumnIndex("hewanidno"));
item.namahewan
= cursor.getString(cursor
.getColumnIndex("namahewan"));
item.Hargahewan
= cursor.getString(cursor
.getColumnIndex("productprice"));
cartList.add(item);
}
while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return
cartList;
}
public
ArrayList<JenisHewan> getHewan(String record) {
cartList.clear();
SQLiteDatabase
db = this.getWritableDatabase();
Cursor
cursor = db.query(true, "producttable", new String[] {
"hewanidno",
"namahewan", "hargahewan" }, "namahewan"
+
"=?", new String[] { record }, null, null, null, null);
if
(cursor.getCount() != 0) {
if
(cursor.moveToFirst()) {
do
{
JenisHewan
item = new JenisHewan();
item.idno
= cursor.getString(cursor
.getColumnIndex("Hewanidno"));
item.namahewan
= cursor.getString(cursor
.getColumnIndex("Namahewan"));
item.hargahewan
= cursor.getString(cursor
.getColumnIndex("hargahewan"));
cartList.add(item);
}
while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return
cartList;
package com.inputhewan.screen;
import com.inputhewan.db.DatabaseHelper;
public class ViewRecord extends Activity {
Menampilkan
record
totalrecords
= (TextView) findViewById(R.id.totalrecords);
listview = (ListView)
findViewById(R.id.listview);
db = new
DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
ArrayList<ProductModel>
product_list = db.getProudcts();
for
(int i = 0; i < product_list.size(); i++) {
String
tidno = hewan_list.get(i).getIdno();
System.out.println("tidno>>>>>"
+ tidno);
String
tnama = product_list.get(i).getNAmahewan();
String
tharga = product_list.get(i).getProductprice();
JenisHewan
_JenisHewan = new JenisHewan();
_JenisHewan.setIdno(tidno);
_JenisHewan.setProductname(tnama);
_JenisHewan
l.setProductprice(tharga);
_hewanlist.add(_JenisHewan);
}
totalrecords.setText("Total
Records :-" + _productlist.size());
listview.setAdapter(new
ListAdapter(this));
db.close();
Pendeklarasian
untuk get dan set produk
package com.inputhewan.screen;
public class JenisHewan
{
public
String getNamahewan() {
return namahewan;
}
public
void setNAmahewan(String namahewan)
{
this.Namahewan = namahewan;
}
public
String getHargahewane() {
return hargahewan;
}
public
void setHargahewan(String hargahewan)
{
this.productprice = productprice;
}
public
String idno="", namahewan="", hargahewan="";
public
String getIdno() {
return idno;
}
public
void setIdno(String idno) {
this.idno = idno;
}
}
Tidak ada komentar:
Posting Komentar