Percobaan 2 : Membuat database dengan SQLHelper

Workshop Struktur dan Pemakaian Teknologi Game 168

b. Percobaan 2 : Membuat database dengan SQLHelper

Langkah 1: Buat project Android baru pada Eclipse Langkah 2: Tambahkan statement berikut pada main.xml ? xml version = 1.0 encoding = utf-8 ? LinearLayout xmlns:android = http:schemas.android.comapkresandroid android:layout_width = fill_parent android:layout_height = fill_parent android:orientation = vertical LinearLayout android:id = +idLinearLayout02 android:layout_width = wrap_content android:layout_height = wrap_content TextView android:id = +idTextView01 android:layout_width = wrap_content android:layout_height = wrap_content android:text = number TextView EditText android:id = +ideditText_num android:layout_width = wrap_content android:layout_height = wrap_content android:phoneNumber = true EditText LinearLayout LinearLayout android:id = +idLinearLayout03 android:layout_width = wrap_content android:layout_height = wrap_content TextView android:id = +idTextView02 android:layout_width = wrap_content android:layout_height = wrap_content android:text = name TextView Workshop Struktur dan Pemakaian Teknologi Game 169 EditText android:id = +ideditText_name android:layout_width = wrap_content android:layout_height = wrap_content EditText LinearLayout LinearLayout android:id = +idLinearLayout01 android:layout_width = wrap_content android:layout_height = wrap_content Button android:id = +idbut_in android:layout_width = wrap_content android:layout_height = wrap_content android:onClick = but_inClick android:text = Add Button Button android:id = +idbut_Edit android:layout_width = wrap_content android:layout_height = wrap_content android:onClick = but_EditClick android:text = Edit Button Button android:id = +idbut_delete android:layout_width = wrap_content android:layout_height = wrap_content android:onClick = but_DelClick android:text = Delete Button LinearLayout TextView android:id = +idTextView03 android:layout_width = wrap_content android:layout_height = wrap_content android:text = DATA TextView ListView Workshop Struktur dan Pemakaian Teknologi Game 170 android:id = +idListView01 android:layout_width = wrap_content android:layout_height = wrap_content ListView LinearLayout Langkah 3 : Buatlah sebuah class xml lagi, beri nama view2.xml ? xml version = 1.0 encoding = utf-8 ? TableLayout xmlns:android = http:schemas.android.comapkresandroid android:id = +idTableLayout01 android:layout_width = fill_parent android:layout_height = fill_parent TextView android:id = +idname android:layout_width = wrap_content android:layout_height = wrap_content android:text = myName TextView TextView android:id = +idnumber android:layout_width = wrap_content android:layout_height = wrap_content android:text = myNumber TextView TableLayout Langkah 4 : Lengkapi statement berikut pada class java MainActivity import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.AdapterView.OnItemClickListener; Workshop Struktur dan Pemakaian Teknologi Game 171 public class usingSQLHelper extends Activity { mySqlHelper dbHelper ; private EditText ed_num ; private EditText ed_name ; protected Cursor cursor ; private int id =-1; protected ListAdapter adapter ; protected ListView numberList ; Called when the activity is first created. Override public void onCreateBundle savedInstanceState { super .onCreatesavedInstanceState; setContentViewR.layout. main ; this . ed_num = EditText this .findViewByIdR.id. editText_num ; this . ed_name = EditText this .findViewByIdR.id. editText_name ; this . numberList = ListView this .findViewByIdR.id. ListView01 ; dbHelper = new mySqlHelper this ; numberList .setSelected true ; numberList .setOnItemClickListener new OnItemClickListener { public void onItemClickAdapterView? arg0, View arg1, int arg2, long arg3 { SQLiteDatabase db = dbHelper .getReadableDatabase; cursor = db.rawQuery SELECT FROM data , null ; cursor .moveToPositionarg2; ed_num .setText cursor .getString1; ed_name .setText cursor .getString2; id = cursor .getInt0; }}; view; } private void addDataString num,String name { SQLiteDatabase db = dbHelper .getWritableDatabase; try { db.execSQL insert into + mySqlHelper. TABLE + valuesnull, +num+ , +name+ ; } catch Exception e { Workshop Struktur dan Pemakaian Teknologi Game 172 ed_num .setTexte.toString; } } public void but_inClickView v { addData ed_num .getText.toString, ed_name .getText.toString; view; } public void but_DelClickView v { SQLiteDatabase db = dbHelper .getWritableDatabase; try { if id =-1 { db.execSQL delete from + mySqlHelper. TABLE + where number= + ed_num .getText.toString+ ; view; } } catch Exception e { ed_num .setTexte.toString; } } public void but_EditClickView v { SQLiteDatabase db = dbHelper .getWritableDatabase; try { if id =-1 { db.execSQL update data set number= + ed_num .getText.toString+ ,name= + ed_name .getText.to String+ where _id= + id ; view; } } catch Exception e { ed_num .setTexte.toString; } } private void view { SQLiteDatabase db = dbHelper .getReadableDatabase; Workshop Struktur dan Pemakaian Teknologi Game 173 try { cursor = db.rawQuery SELECT FROM data , null ; adapter = new SimpleCursorAdapter this , R.layout. view2 , cursor , new String[] { number , name }, new int [] {R.id. number ,R.id. name }; numberList .setAdapter adapter ;} catch Exception e { ed_num .setTexte.toString; } } } Langkah 5 : Kemudian, buatlah sebuah class java baru, beri nama mySqlHelper.java import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class mySqlHelper extends SQLiteOpenHelper{ private static final String DATABASE_NAME = mydatabase.db ; private static final int DATABASE_VERSION = 1; Table name public static final String TABLE = data ; Columns public static final String number = number ; public static final String name = name ; public mySqlHelperContext context { super context, DATABASE_NAME , null , DATABASE_VERSION ; TODO Auto-generated constructor stub } Override public void onCreateSQLiteDatabase db { String sql = create table + TABLE + _id Workshop Struktur dan Pemakaian Teknologi Game 174 + integer primary key autoincrement, + number + text not null, + name + text not null; ; Log.d Data , onCreate: + sql; db.execSQLsql; } Override public void onUpgradeSQLiteDatabase db, int oldVersion, int newVersion { TODO Auto-generated method stub } } Lakukan editing pada kode yang dicetak tebal berikut pada class AndroidManifest.xml: ? xml version = 1.0 encoding = utf-8 ? manifest xmlns:android = http:schemas.android.comapkresandroid package = com.data android:versionCode = 1 android:versionName = 1.0 uses-sdk android:minSdkVersion = 10 application android:icon = drawableic_launcher android:label = stringapp_name activity android:name = .DatabaseBukuActivity android:label = stringapp_name intent-filter action android:name = android.intent.action.MAIN category android:name = android.intent.category.LAUNCHER intent-filter activity application manifest Langkah 6 :Tekan F11 untuk mendebug aplikasi pada emulator Android Langkah 7 : Maka akan muncul outputnya Workshop Struktur dan Pemakaian Teknologi Game 175

c. Percobaan 3 : Membuat database dengan ContentService