Berikut adalah struktur file program penjualan, perhatikan bagian src java dan layout.
DataHelper.java
package ubj1.com.crudbarang; /** * Created by HP on 27/05/2018. */ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DataHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "penjualan.db"; private static final int DATABASE_VERSION = 1; public DataHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS barang"); db.execSQL("DROP TABLE IF EXISTS biodata"); db.execSQL("DROP TABLE IF EXISTS pesanan"); db.execSQL("CREATE TABLE IF NOT EXISTS barang(kd integer primary key, nama text null, harga integer null)"); db.execSQL("INSERT INTO barang (kd, nama, harga) VALUES ('1001', 'Televisi Toshiba 32 Inch', '3500000')"); db.execSQL("INSERT INTO barang (kd, nama, harga) VALUES ('1002', 'Lemari ES Sharp 2 Pintu', '4700000')"); db.execSQL("CREATE TABLE IF NOT EXISTS pesanan(id integer primary key, tanggal text null, no integer null, kd integer null, qty integer null)"); db.execSQL("INSERT INTO pesanan (id, tanggal, no, kd, qty) VALUES ('101', '2018-05-23', '100', '1001', '5')"); db.execSQL("INSERT INTO pesanan (id, tanggal, no, kd, qty) VALUES ('102', '2018-05-24', '100', '1002', '3')"); String sql = "CREATE TABLE IF NOT EXISTS biodata(no integer primary key, nama text null, tgl text null, jk text null, alamat text null);"; Log.d("Data", "onCreate: " + sql); db.execSQL(sql); sql = "INSERT INTO biodata (no, nama, tgl, jk, alamat) VALUES ('100', 'Dani Yusuf', '1996-07-12', 'Laki-laki','Jakarta');"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS barang"); db.execSQL("DROP TABLE IF EXISTS biodata"); db.execSQL("DROP TABLE IF EXISTS pesanan"); onCreate(db); } }
Untuk scrip menampilkan data penjulan adalah sebagai berikut: package ubj1.com.crudbarang; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class LihatPesanan extends AppCompatActivity { protected Cursor cursor, cursor2, cursor3; DataHelper dbHelper; Button ton2; TextView text1, text2, text3, text4, text5, text6, text11, text12, text13; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lihat_pesanan); dbHelper = new DataHelper(this); text1 = (TextView) findViewById(R.id.textView1); text2 = (TextView) findViewById(R.id.textView2); text3 = (TextView) findViewById(R.id.textView3); text4 = (TextView) findViewById(R.id.textView4); text5 = (TextView) findViewById(R.id.textView5); text6 = (TextView) findViewById(R.id.textView6); text11 = (TextView) findViewById(R.id.textView11); text12 = (TextView) findViewById(R.id.textView12); text13 = (TextView) findViewById(R.id.textView13); SQLiteDatabase db = dbHelper.getReadableDatabase(); cursor = db.rawQuery("SELECT * FROM pesanan WHERE id = '" + getIntent().getStringExtra("id") + "'",null); cursor.moveToFirst(); if (cursor.getCount()>0) { cursor.moveToPosition(0); text1.setText(cursor.getString(0).toString()); text2.setText(cursor.getString(1).toString()); text3.setText(cursor.getString(2).toString()); text4.setText(cursor.getString(3).toString()); text5.setText(cursor.getString(4).toString()); //int qty = Integer.parseInt(cursor.getString(4).toString()); //String kode_barang = cursor.getString(3); //text8.setText(String.valueOf(kode_barang)); //int ntext6 = qty*3500000; //text6.setText(String.valueOf(ntext6)); } cursor2 = db.rawQuery("SELECT * FROM barang WHERE kd = '" + cursor.getString(3).toString() + "'",null); cursor2.moveToFirst(); if (cursor2.getCount()>0) { cursor2.moveToPosition(0); text12.setText(cursor2.getString(1).toString()); text13.setText(cursor2.getString(2).toString()); int harganya = Integer.parseInt(cursor2.getString(2).toString()); } int qty = Integer.parseInt(cursor.getString(4).toString()); int harganya = Integer.parseInt(cursor2.getString(2).toString()); int ntext6 = harganya * qty; text6.setText(String.valueOf(ntext6)); cursor3 = db.rawQuery("SELECT * FROM biodata WHERE no = '" + cursor.getString(2).toString() + "'",null); cursor3.moveToFirst(); if (cursor3.getCount()>0) { cursor3.moveToPosition(0); text11.setText(cursor3.getString(1).toString()); } ton2 = (Button) findViewById(R.id.button1); ton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); } }
MainActivity.java untuk mengatur perpindahan activity dari halaman utama ke halaman Master Barang, Master Pelanggan dan Transaksi Penjualan.
package ubj1.com.crudbarang; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btn_barang, btn_biodata, btn_pesanan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_barang = (Button) findViewById(R.id.btn_barang); btn_barang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent (MainActivity.this, ActivityKedua.class); startActivity(i); } }); btn_biodata = (Button) findViewById(R.id.btn_biodata); btn_biodata.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent (MainActivity.this, ActivityKetiga.class); startActivity(i); } }); btn_pesanan = (Button) findViewById(R.id.btn_pesanan); btn_pesanan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent (MainActivity.this, ActivityKeempat.class); startActivity(i); } }); } }
Untuk layout activity_main.xml adalah sebagai berikut:
xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout_utama" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/idcarddani" android:orientation="vertical" tools:context="ubj1.com.crudbarang.MainActivity">
<Button android:id="@+id/btn_barang" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btn_biodata" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:background="@android:color/holo_blue_dark" android:text="MASTER BARANG" />
<Button android:id="@+id/btn_pesanan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@android:color/holo_blue_dark" android:text="Transaksi Pesanan" />
<Button android:id="@+id/btn_biodata" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btn_pesanan" android:layout_centerHorizontal="true" android:background="@android:color/holo_blue_light" android:text="Master Pelanggan" />
</RelativeLayout>
Comments