return true; default:
return super.onOptionsItemSelecteditem; }
} }
2. Kelas DBAdapter.java
package com.friska.makanan; import android.content.Context;
import android.database.Cursor; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log; public class DBAdapter {
public static final String KEY_ID = _id; public static final String KEY_NAME = nama;
public static final String KEY_KET = ket; public static final String TAG = DBAdapter;
private static final String DATABASE_NAME = masakan.db; private static final String TABLE_IKAN = ikan;
private static final String TABLE_SAYUR = sayur; private static final String TABLE_TEPUNG = tepung;
private static final String TABLE_DAGING = daging; private static final int DATABASE_VERSION = 2;
Universitas Sumatera Utara
private static final String IKAN = create table ikan _id integer primary key
autoincrement, + nama text not null, ket text not null;;
private static final String SAYUR = create table sayur _id integer primary key autoincrement,
+ nama text not null, ket text not null;;
private static final String TEPUNG = create table tepung _id integer primary key autoincrement,
+ nama text not null, ket text not null;;
private static final String DAGING = create table daging _id integer primary key autoincrement,
+ nama text not null, ket text not null;;
private final Context context; private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapterContext ctx {
this.context = ctx; DBHelper = new DatabaseHelpercontext;
}
private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelperContext context {
supercontext, DATABASE_NAME, null, DATABASE_VERSION; }
Universitas Sumatera Utara
Override public void onCreateSQLiteDatabase db {
try { db.execSQLIKAN;
db.execSQLTEPUNG; db.execSQLSAYUR;
db.execSQLDAGING; } catch SQLException e {
e.printStackTrace; }
}
Override public void onUpgradeSQLiteDatabase db, int oldVersion, int
newVersion {
Log.wTAG, Upgrade database dari versi + oldVersion + ke + newVersion + , yang akan menghapus semua data lama;
db.execSQLDROP TABLE IF EXISTS sayur; onCreatedb;
} }
---membuka database--- public DBAdapter open throws SQLException
{ db = DBHelper.getWritableDatabase;
return this; }
---menutup database--- public void close
Universitas Sumatera Utara
{ DBHelper.close;
}
menampilkan semua isi tabel database public Cursor getAllSayur
{ return db.queryTABLE_SAYUR, new String[] {KEY_ID, KEY_NAME,
KEY_KET}, null, null, null, null, null; }
public Cursor getAllIkan {
return db.queryTABLE_IKAN, new String[] {KEY_ID, KEY_NAME, KEY_KET}, null, null, null, null, null;
} public Cursor getAllTepung
{ return db.queryTABLE_TEPUNG, new String[] {KEY_ID, KEY_NAME,
KEY_KET}, null, null, null, null, null; }
public Cursor getAllDaging {
return db.queryTABLE_DAGING, new String[] {KEY_ID, KEY_NAME, KEY_KET}, null, null, null, null, null;
}
menampilkan sebuah isi tabel database public Cursor getSingleSayurint id throws SQLException
{ Cursor cursor = db.querytrue, TABLE_SAYUR, new String[] {KEY_ID,
KEY_NAME, KEY_KET}, KEY_ID + = + id, null, null, null, null, null; if cursor = null {
cursor.moveToFirst; }
Universitas Sumatera Utara
return cursor; }
public Cursor getSingleIkanint id throws SQLException {
Cursor cursor = db.querytrue, TABLE_IKAN, new String[] {KEY_ID, KEY_NAME, KEY_KET}, KEY_ID + = + id, null, null, null, null, null;
if cursor = null { cursor.moveToFirst;
} return cursor;
} public Cursor getSingleTepungint id throws SQLException
{ Cursor cursor = db.querytrue, TABLE_TEPUNG, new String[]
{KEY_ID, KEY_NAME, KEY_KET}, KEY_ID + = + id, null, null, null, null, null;
if cursor = null { cursor.moveToFirst;
} return cursor;
}
public Cursor getSingleDagingint id throws SQLException {
Cursor cursor = db.querytrue, TABLE_DAGING, new String[] {KEY_ID, KEY_NAME, KEY_KET}, KEY_ID + = + id, null, null, null,
null, null;
if cursor = null { cursor.moveToFirst;
} return cursor;
} }
Universitas Sumatera Utara
3. Kelas DashboardSetting.java