Create

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bTambah, bLihat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bTambah = (Button) findViewById(R.id.button_tambah);
        bTambah.setOnClickListener(this);

        bLihat = (Button) findViewById(R.id.button_view);
        bLihat.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_tambah:
                Intent i = new Intent(this, TambahDataActivity.class);
                startActivity(i);
                break;
            case R.id.button_view:
                Intent i2 = new Intent(this, TampilDataActivity.class);
                startActivity(i2);
                break;
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/nama_app"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="CRUD"
        android:textSize="20sp" />

    <Button
        android:layout_marginTop="20dp"
        android:id="@+id/button_tambah"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nama_app"
        android:layout_centerHorizontal="true"
        android:text="Tambah Data" />

    <Button
        android:id="@+id/button_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_tambah"
        android:layout_centerHorizontal="true"
        android:text="Lihat Data"
        />
</LinearLayout>

public class DBHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "data_inventori";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "nama_barang";
    public static final String COLUMN_MERK = "merk_barang";
    public static final String COLUMN_HARGA = "harga_barang";
    private static final String db_name ="inventori.db";
    private static final int db_version=1;

    private static final String db_create = "create table "
            + TABLE_NAME + "("
            + COLUMN_ID +" integer primary key autoincrement, "
            + COLUMN_NAME+ " varchar(50) not null, "
            + COLUMN_MERK+ " varchar(50) not null, "
            + COLUMN_HARGA+ " varchar(50) not null);";

    public DBHelper(Context context) {
        super(context, db_name, null, db_version);
        // Auto generated
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(db_create);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DBHelper.class.getName(),"Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

    }

}

public class DBSource {

    private SQLiteDatabase database;

    private DBHelper dbHelper;

    private String[] allColumns = {DBHelper.COLUMN_ID,
            DBHelper.COLUMN_NAME, DBHelper.COLUMN_MERK, DBHelper.COLUMN_HARGA};

    public DBSource(Context context) {
        dbHelper = new DBHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public Barang createBarang(String nama, String merk, String harga) {

        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_NAME, nama);
        values.put(DBHelper.COLUMN_MERK, merk);
        values.put(DBHelper.COLUMN_HARGA, harga);

        long insertId = database.insert(DBHelper.TABLE_NAME, null,
                values);

        Cursor cursor = database.query(DBHelper.TABLE_NAME,
                allColumns, DBHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);

        cursor.moveToFirst();


        Barang newBarang = cursorToBarang(cursor);
        cursor.close();

        return newBarang;
    }

    private Barang cursorToBarang(Cursor cursor) {
        Barang barang = new Barang();
        // debug LOGCAT
        Log.v("info", "The getLONG " + cursor.getLong(0));
        Log.v("info", "The setLatLng " + cursor.getString(1) + "," + cursor.getString(2));


        barang.setId(cursor.getLong(0));
        barang.setNama_barang(cursor.getString(1));
        barang.setMerk_barang(cursor.getString(2));
        barang.setHarga_barang(cursor.getString(3));

        return barang;
    }
}

public class Barang {

    private long id;
    private String nama_barang;
    private String merk_barang;
    private String harga_barang;

    public Barang() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNama_barang() {
        return nama_barang;
    }

    public void setNama_barang(String nama_barang) {
        this.nama_barang = nama_barang;
    }

    public String getMerk_barang() {
        return merk_barang;
    }

    public void setMerk_barang(String merk_barang) {
        this.merk_barang = merk_barang;
    }

    public String getHarga_barang() {
        return harga_barang;
    }

    public void setHarga_barang(String harga_barang) {
        this.harga_barang = harga_barang;
    }

    @Override
    public String toString() {
        return "Barang "+ nama_barang +" "+ merk_barang + " "+ harga_barang;
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/nama_barang"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="Nama Barang"
        android:inputType="text">
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/merk_barang"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="Merk Barang"
        android:inputType="text">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/harga_barang"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="Harga Barang"
        android:inputType="number">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/buttom_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />
</LinearLayout>
public class TambahDataActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonSubmit;
    private EditText edNama;
    private EditText edMerk;
    private EditText edHarga;

    private DBSource dataSource;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tambah_data);

        buttonSubmit = (Button) findViewById(R.id.buttom_submit);
        buttonSubmit.setOnClickListener(this);
        edNama = (EditText) findViewById(R.id.nama_barang);
        edHarga = (EditText) findViewById(R.id.harga_barang);
        edMerk = (EditText) findViewById(R.id.merk_barang);

        dataSource = new DBSource(this);

        dataSource.open();
    }

    @Override
    public void onClick(View v) {
        String nama = null;
        String merk = null;
        String harga = null;

                Barang barang = null;
        if (edNama.getText() != null && edMerk.getText() != null && edHarga.getText() != null) {
            nama = edNama.getText().toString();
            merk = edMerk.getText().toString();
            harga = edHarga.getText().toString();
        }

        switch (v.getId()) {
            case R.id.buttom_submit:
                barang = dataSource.createBarang(nama, merk, harga);

                Toast.makeText(this, "masuk Barang\n" +
                        "nama : " + barang.getNama_barang() +
                        " merk : " + barang.getMerk_barang() +
                        " harga : " + barang.getHarga_barang(), Toast.LENGTH_LONG).show();
                break;
        }

    }
}

Last updated

Was this helpful?