Cara Buat Aplikasi Android Input Data (Tab_Layout)
Ini Source Code Untuk MainActivity.java
import java.util.ArrayList;
import java.util.List;
import com.riski.inputdata.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
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.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
List<almag> model = new ArrayList<almag>();
almagAdapter adapter = null;
EditText nim = null;
EditText nama = null;
EditText judulbuku = null;
RadioGroup kelas = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nim = (EditText)findViewById(R.id.nim);
nama = (EditText) findViewById(R.id.nama);
judulbuku = (EditText) findViewById(R.id.jb);
kelas = (RadioGroup) findViewById(R.id.kelas);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView) findViewById(R.id.inputdata);
adapter = new almagAdapter();
list.setAdapter(adapter);
TabHost.TabSpec spec = getTabHost().newTabSpec("tag1");
spec.setContent(R.id.inputdata);
spec.setIndicator("PEMINJAM", getResources().getDrawable(R.drawable.list));
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tag2");
spec.setContent(R.id.peminjam);
spec.setIndicator("INPUT DATA",
getResources().getDrawable(R.drawable.details));
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
list.setOnItemClickListener(onListClick);
}
private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
almag r = new almag();
r.setNim(nim.getText().toString());
r.setNama(nama.getText().toString());
r.setJudulbuku(judulbuku.getText().toString());
switch (kelas.getCheckedRadioButtonId()) {
case R.id.a:
r.setJekel("A");
break;
case R.id.b:
r.setJekel("B");
break;
}
adapter.add(r);
}
};
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
almag r = model.get(position);
nim.setText(r.getNim());
nama.setText(r.getNama());
judulbuku.setText(r.getJudulbuku());
if (r.getKelas().equals("A")) {
kelas.check(R.id.a);
} else if (r.getKelas().equals("B")) {
kelas.check(R.id.b);
}
getTabHost().setCurrentTab(1);
}
};
class almagAdapter extends ArrayAdapter<almag> {
almagAdapter() {
super(MainActivity.this, R.layout.row, model);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
almagHolder holder = null;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
holder = new almagHolder(row);
row.setTag(holder);
} else {
holder = (almagHolder) row.getTag();
}
holder.populateFrom(model.get(position));
return (row);
}
}
static class almagHolder {
private TextView nim = null;
private TextView nama = null;
private TextView judulbuku = null;
private ImageView icon = null;
private View row = null;
almagHolder(View row) {
this.row = row;
nim = (TextView) row.findViewById(R.id.l_nim);
nama = (TextView) row.findViewById(R.id.title);
judulbuku = (TextView) row.findViewById(R.id.jb);
icon = (ImageView) row.findViewById(R.id.icon);
}
void populateFrom(almag r) {
nim.setText(r.getNim());
nama.setText(r.getNama());
judulbuku.setText(r.getJudulbuku());
if (r.getKelas().equals("A")) {
icon.setImageResource(R.drawable.a);
} else if (r.getKelas().equals("B")) {
icon.setImageResource(R.drawable.a);
}
}
}
}
Ini Adalah Source Code Untuk Almag.java
public class almag {
private String nim = "";
private String nama = "";
private String judulbuku = "";
private String kelas = "";
public String getNim() {
return (nim);
}
public void setNim(String nim) {
this.nim = nim;
}
public String getNama() {
return (nama);
}
public void setNama(String nama) {
this.nama = nama;
}
public String getJudulbuku() {
return (judulbuku);
}
public void setJudulbuku(String judulbuku) {
this.judulbuku = judulbuku;
}
public String getKelas() {
return (kelas);
}
public void setJekel(String kelas) {
this.kelas = kelas;
}
public String toString() {
return (getNama());
}
}
Dan Ini Adalah Source Code Untuk main.xml
<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:background="@drawable/b"
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/inputdata"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TableLayout
android:id="@+id/peminjam"
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"
android:numeric="integer"/>
</TableRow>
<TableRow >
<TextView android:text="Nama:" />
<EditText android:id="@+id/nama" />
</TableRow>
<TableRow>
<TextView android:text="Kelas:" />
<RadioGroup android:id="@+id/kelas" >
<RadioButton
android:id="@+id/a"
android:text="A" />
<RadioButton
android:id="@+id/b"
android:text="B" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:text="Judul buku:" />
<EditText android:id="@+id/jb" />
</TableRow>
<Button
android:id="@+id/save"
android:layout_marginTop="40dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff9999ff"
android:text="Save" />
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Next Ini Adalah Source Code Untuk 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_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="4px" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/l_nim"
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"
android:textStyle="bold" />
<TextView
android:id="@+id/jb"
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>
Dan Ini Adalah Hasil Running Dari Source Code Di Atas
Komentar
Posting Komentar