CARA MEMBUAT SQLITE DATABASE INPUT DATA

Assalamualaikum wr wb....

  1. Buatlah project dengan nama TugasArrayAdapter
    Activity Name = MainActivity
    Layout Name =  activity_main
  2. Jika sudah membuat project. kita membuat file AlmagHelper.java pada folder src dan file xml pada folde layout dan land sehingga tampilan filenya seperti berikut :
2
3. Masukan source code berikut pada layout/activity_main.xml :
<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@android:id/tabhost”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<TabWidget
android:id=”@android:id/tabs”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<FrameLayout
android:id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<ListView
android:id=”@+id/almag”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” />
<TableLayout
android:id=”@+id/details”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:paddingTop=”4px”
android:stretchColumns=”1″ >
<TableRow>
<TextView
android:text=”NIM :”
android:textSize=”12dp”
android:textStyle=”bold” />
<EditText android:id=”@+id/nim” />
</TableRow>
<TableRow>
<TextView
android:text=”Nama:”
android:textSize=”12dp”
android:textStyle=”bold” />
<EditText android:id=”@+id/nama” />
</TableRow>
<TableRow>
<TextView
android:text=”Semester”
android:textSize=”12dp”
android:textStyle=”bold” />
<RadioGroup android:id=”@+id/semester” >
<RadioButton
android:id=”@+id/ganjil”
android:text=”Ganjil”
android:textSize=”12dp”
android:textStyle=”bold” />
<RadioButton
android:id=”@+id/genap”
android:text=”Genap”
android:textSize=”12dp”
android:textStyle=”bold” />
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:text=”Matakuliah :”
android:textSize=”16dp”
android:textStyle=”bold”/>
<EditText android:id=”@+id/matakuliah” />
</TableRow>
<TableLayout
android:id=”@+id/tableLayout1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/imageView1″
android:layout_marginTop=”10dp”
android:shrinkColumns=”*”
android:stretchColumns=”*” >
<TableRow
android:id=”@+id/tableRowBtn”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:paddingBottom=”7dp” >
<Button
android:id=”@+id/save”
android:layout_width=”50dp”
android:layout_height=”50dp”
android:layout_margin=”2dp”
android:layout_weight=”1″
android:background=”@color/colorGrassDark”
android:text=”Insert Data”
android:textColor=”@color/colorWhite”
android:textSize=”16dp”
android:textStyle=”italic” />
<Button
android:id=”@+id/reset”
android:layout_width=”50dp”
android:layout_height=”50dp”
android:layout_margin=”2dp”
android:layout_weight=”1″
android:background=”@color/colorGrassDark”
android:text=”Reset”
android:textColor=”@color/colorWhite”
android:textSize=”16dp”
android:textStyle=”italic” />
</TableRow>
</TableLayout>
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
4.Masukan coding berikut pada layout/row.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:padding=”4px” >
<TextView
android:id=”@+id/no1″
android:layout_width=”wrap_content”
android:layout_height=”fill_parent”
android:text=”Large Text”
android:layout_alignParentBottom=”true”
android:layout_alignParentTop=”true”
android:layout_marginRight=”4px”
android:textAppearance=”?android:attr/textAppearanceLarge” />
<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical” >
<TextView
android:id=”@+id/nim1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:ellipsize=”end”
android:gravity=”center_vertical”
android:singleLine=”true”
android:textStyle=”bold” />
<TextView
android:id=”@+id/title”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:ellipsize=”end”
android:gravity=”center_vertical”
android:singleLine=”true” />
<TextView
android:id=”@+id/mata_k”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:ellipsize=”end”
android:gravity=”center_vertical”
android:singleLine=”true” />
</LinearLayout>
</LinearLayout>
5.Masukan Coding pada layout/layout_land.xml :
<?xml version=”1.0″ encoding=”utf-8″?><?xml version=”1.0″ encoding=”utf-8″?>
<TabHost xmlns:android=”http://schemas.android.com/apk/res/android”    android:id=”@android:id/tabhost”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<TabWidget
android:id=”@android:id/tabs”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />
<FrameLayout
android:id=”@android:id/tabcontent”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<ListView
android:id=”@+id/almag”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” />
<TableLayout
android:id=”@+id/details”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:paddingTop=”4px”
android:stretchColumns=”1″ >
<TableRow>
<TextView android:text=”NIM :” />
<EditText android:id=”@+id/nim” />
</TableRow>
<TableRow>
<TextView android:text=”Nama:” />
<EditText android:id=”@+id/nama” />
</TableRow>
<TableRow>
<TextView android:text=”Semester” />
<RadioGroup android:id=”@+id/semester” >
<RadioButton
android:id=”@+id/ganjil”
android:text=”Ganjil” />
<RadioButton
android:id=”@+id/genap”
android:text=”Genap” />
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:text=”Matakuliah :” />
<EditText android:id=”@+id/matakuliah”/>
</TableRow>
<Button
android:id=”@+id/save”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Save” />
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

6.Masukan Coding berikut pada src/com.wilis.tugasarrayadapter/MainActivity.java :
package com.example.tugasarrayadapter;
package com.example.tugasarrayadapter;
import java.util.ArrayList;import java.util.List;
import android.os.Bundle;import android.app.Activity;
import android.app.TabActivity;import android.content.Context;
import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;
import android.graphics.YuvImage;import android.support.v4.widget.CursorAdapter;import android.view.LayoutInflater;
import android.view.Menu;import android.view.View;
import android.view.ViewGroup;import android.widget.AdapterView;
import android.widget.ArrayAdapter;import android.widget.Button;
import android.widget.EditText;import android.widget.ImageView;
import android.widget.ListView;import android.widget.RadioGroup;
import android.widget.Spinner;import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {      Cursor model=null;
AlmagAdapter adapter=null;
EditText nama = null;    EditText nim = null;
RadioGroup semester = null;
EditText matakuliah;
AlmagHelper helper=null;
Button reset;
@Override
public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Script Untuk Mereset Isi dari EditText
Button reset = (Button)findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nim.setText(“”);
nama.setText(“”);
matakuliah.setText(“”);
} });
helper=new AlmagHelper(this);
nim = (EditText)findViewById(R.id.nim);
nama = (EditText)findViewById(R.id.nama);
matakuliah =(EditText)findViewById(R.id.matakuliah);
semester = (RadioGroup)findViewById(R.id.semester);
Button save = (Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView)findViewById(R.id.almag);
model=helper.getAll();
startManagingCursor(model);
adapter=new AlmagAdapter(model);
list.setAdapter(adapter);
TabHost.TabSpec spec=getTabHost().newTabSpec(“tag1”);                spec.setContent(R.id.almag);
spec.setIndicator(“List”, getResources().getDrawable(R.drawable.biodata));        getTabHost().addTab(spec);
spec=getTabHost().newTabSpec(“tag2”);
spec.setContent(R.id.details);        spec.setIndicator(“Biodata”,getResources().getDrawable(R.drawable.datamahasiswa));        getResources().getDrawable(R.drawable.datamahasiswa);                getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
list.setOnItemClickListener(onListClick);
}
public void onDestory(){
super.onDestroy();
helper.close();    }
private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
String type =null;
switch (semester.getCheckedRadioButtonId()) {
case R.id.ganjil: type=”Ganjil”;
break;
case R.id.genap: type=”Genap”;
break;
}
helper.insert(nim.getText().toString(), nama.getText().toString(),type,matakuliah.getText().toString());
model.requery();
}
};
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,                long id) {            model.moveToPosition(position);            nim.setText(helper.getNim(model));            nama.setText(helper.getAlamat(model));            matakuliah.setText(helper.getMatakuliah(model));
if (helper.getSemester(model).equals(“Ganjil”)){
semester.check(R.id.ganjil);
}else if (helper.getSemester(model).equals(“Genap”)){
semester.check(R.id.genap);
}
getTabHost().setCurrentTab(1);
}    };
class AlmagAdapter extends CursorAdapter {
AlmagAdapter(Cursor c) {
super(MainActivity.this, c);
}
public void bindView(View row, Context ctxt, Cursor c){
almagHolder holder = (almagHolder)row.getTag();
holder.populateFrom( c, helper);
}
public View newView(Context ctxt, Cursor c, ViewGroup parent){
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
almagHolder holder = new almagHolder(row);
row.setTag(holder);
return(row);
}
}
static class almagHolder {
private TextView nim = null;
private TextView nama = null;
private TextView matakuliah = null;
private TextView no = null;
private View row = null;
almagHolder(View row) {
this.row = row;
nim = (TextView) row.findViewById(R.id.nim1);
nama = (TextView) row.findViewById(R.id.title);
matakuliah = (TextView) row.findViewById(R.id.mata_k);
no = (TextView) row.findViewById(R.id.no1);                    }
void populateFrom(Cursor c, AlmagHelper helper) {        no.setText(“@”);        nim.setText(helper.getNim(c));
nama.setText(helper.getAlamat(c));
matakuliah.setText(helper.getMatakuliah(c));
if (helper.getSemester(c).equals(“Ganjil”)){
}else if (helper.getSemester(c).equals(“Genap”)){
}
}
public void hapus_biodata(int id){ SQLiteDatabase database = this.getWritableDatabase(); database.execSQL(“DELETE FROM tabel_biodata WHERE id_biodata=’”+id+”‘”); database.close();
}
private SQLiteDatabase getWritableDatabase() { /
/ TODO Auto-generated method stub
return null;
}
}
}
7.Masukan Coding berikut pada src/com.wilis.tugasarrayadapter/Almagjava :
package com.example.tugasarrayadapter;
package com.example.tugasarrayadapter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AlmagHelper extends SQLiteOpenHelper{
//Disini Adalah Fungsi Utama Fungsi SQlite tempat pembuatan database
//Definisi Database Serta Struktur Table didefinisikan dari bagian script di bawah ini. //Nama database adalah addressmanager.db, dengan nama tabel almag,
//yang berisi field id,nim,nama,semester dan matakuliah. private static final String DATABASE_NAME=”data.db”;
private static final int SCHEMA_VERSION=1;
public AlmagHelper(Context context){ super(context, DATABASE_NAME, null, SCHEMA_VERSION); }
public void onCreate(SQLiteDatabase db){
db.execSQL(“CREATE TABLE almag(_id INTEGER PRIMARY KEY AUTOINCREMENT, nim TEXT, nama TEXT, semester TEXT, matakuliah TEXT);”); }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub } public Cursor getAll(){
return (getReadableDatabase() .rawQuery(“SELECT _id, nim, nama, semester, matakuliah FROM almag ORDER BY nim “, null)); }
public void insert(String nim, String nama, String semester, String matakuliah){ ContentValues cv =new ContentValues();
cv.put(“nim”, nim);
cv.put(“nama”, nama);
cv.put(“semester”, semester);
cv.put(“matakuliah”, matakuliah);
getWritableDatabase().insert(“almag”, “name”, cv); }
public String getNim(Cursor c){ return(c.getString(1));
}
public String getAlamat(Cursor c){ return(c.getString(2)); }
public String getSemester(Cursor c){ return(c.getString(3)); }
public String getMatakuliah(Cursor c){ return(c.getString(4)); }
}
8. Jika telah selesai memodifikasi coding dari beberapa file cobalah running project yang    telah dibuat
Hasil running awal :
Capture4.JPG

Tidak ada komentar:

Posting Komentar

Pages