RAB Database
RAB Database
main.xml
/>
</TableRow>
<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
/>
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4px"
>
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="4px"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
/>
<TextView android:id="@+id/alamat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="end"
/>
</LinearLayout>
</LinearLayout>
Buat folder layout-land didalam folder res, kemudian buat file main.xml didalam folder res/layout-
land
main.xml
android:scrollHorizontally="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
/>
</LinearLayout>
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Buatlah file option.xml pada folder res/menu
option.xml
AlmagHelper.java
package com.user.database;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY
AUTOINCREMENT, nama TEXT, alamat TEXT, jekel TEXT, hp TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}
cv.put("nama", nama);
cv.put("alamat", alamat);
cv.put("jekel", jekel);
cv.put("hp", hp);
DatabaseExample.java
package com.wilis.database2;
import android.app.TabActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new AlmagHelper(this);
nama=(EditText)findViewById(R.id.nama);
alamat=(EditText)findViewById(R.id.alamat);
hp=(EditText)findViewById(R.id.hp);
jekel=(RadioGroup)findViewById(R.id.jekel);
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.list));
getTabHost().addTab(spec);
spec=getTabHost().newTabSpec("tag2");
spec.setContent(R.id.details);
spec.setIndicator("Details",
getResources().getDrawable(R.drawable.alamat));
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
list.setOnItemClickListener(onListClick);
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
helper.insert(nama.getText().toString(),alamat.getText().toString(),type,hp
.getText().toString());
model.requery();
}
};
long id) {
model.moveToPosition(position);
nama.setText(helper.getNama(model));
alamat.setText(helper.getAlamat(model));
hp.setText(helper.getHp(model));
if (helper.getJekel(model).equals("Pria")) {
jekel.check(R.id.pria);
}
else if (helper.getJekel(model).equals("Perempuan")) {
jekel.check(R.id.perempuan);
}
getTabHost().setCurrentTab(1);
}
};
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
AlmagHolder holder=(AlmagHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
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);
}
}
AlmagHolder(View row) {
this.row=row;
nama=(TextView)row.findViewById(R.id.title);
alamat=(TextView)row.findViewById(R.id.alamat);
icon=(ImageView)row.findViewById(R.id.icon);
}
if (helper.getJekel(c).equals("Pria")) {
icon.setImageResource(R.drawable.pria);
}
else if (helper.getJekel(c).equals("Perempuan")) {
icon.setImageResource(R.drawable.perempuan);
}
}
}
}