membuat sebuah array pertama kita harus membuat sebuah class disini saya memberi nama dengan almag dan tambahkan scrip berikut kedalam class almag
package com.example.array3;
public class Almag {
private String nama = “”;
private String alamat = “”;
private String jekel = “”;
private String nama = “”;
private String alamat = “”;
private String jekel = “”;
public String getNama() {
return (nama);
}
return (nama);
}
public void setNama(String nama) {
this.nama = nama;
}
this.nama = nama;
}
public String getAlamat() {
return (alamat);
}
return (alamat);
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
public String getJekel() {
return (jekel);
}
public void setJekel(String jekel) {
this.jekel = jekel;
}
public String toString() {
return (getNama());
}
public Object getSemester() {
// TODO Auto-generated method stub
return (getSemester());
}
}
this.alamat = alamat;
}
public String getJekel() {
return (jekel);
}
public void setJekel(String jekel) {
this.jekel = jekel;
}
public String toString() {
return (getNama());
}
public Object getSemester() {
// TODO Auto-generated method stub
return (getSemester());
}
}
kemudian kita buat class baru lagi, disini saya membuat dua class dimana class yang ke dua saya beri nama array dan tambakan script berikut ini juga
package com.example.array3;
import java.util.ArrayList;
import java.util.List;
import com.example.array3.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;
import java.util.List;
import com.example.array3.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 array3 extends TabActivity {
List<Almag> model = new ArrayList<Almag>();
ArrayAdapter adapter = null;
EditText nama = null;
EditText alamat = null;
RadioGroup jekel = null;
List<Almag> model = new ArrayList<Almag>();
ArrayAdapter adapter = null;
EditText nama = null;
EditText alamat = null;
RadioGroup jekel = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nama = (EditText) findViewById(R.id.nama);
alamat = (EditText) findViewById(R.id.alamat);
jekel = (RadioGroup) findViewById(R.id.jekel);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView) findViewById(R.id.almag);
adapter = new AlmagAdapter();
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);
}
private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
Almag r = new Almag();
r.setNama(nama.getText().toString());
r.setAlamat(alamat.getText().toString());
switch (jekel.getCheckedRadioButtonId()) {
case R.id.pria:
r.setJekel(“Pria”);
break;
case R.id.perempuan:
r.setJekel(“Perempuan”);
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);
nama.setText(r.getNama());
alamat.setText(r.getAlamat());
if (r.getJekel().equals(“Pria”)) {
jekel.check(R.id.pria);
} else if (r.getJekel().equals(“Perempuan”)) {
jekel.check(R.id.perempuan);
alamat = (EditText) findViewById(R.id.alamat);
jekel = (RadioGroup) findViewById(R.id.jekel);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView) findViewById(R.id.almag);
adapter = new AlmagAdapter();
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);
}
private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
Almag r = new Almag();
r.setNama(nama.getText().toString());
r.setAlamat(alamat.getText().toString());
switch (jekel.getCheckedRadioButtonId()) {
case R.id.pria:
r.setJekel(“Pria”);
break;
case R.id.perempuan:
r.setJekel(“Perempuan”);
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);
nama.setText(r.getNama());
alamat.setText(r.getAlamat());
if (r.getJekel().equals(“Pria”)) {
jekel.check(R.id.pria);
} else if (r.getJekel().equals(“Perempuan”)) {
jekel.check(R.id.perempuan);
}
getTabHost().setCurrentTab(1);
}
};
private View.OnClickListener onSave1 = new View.OnClickListener() {
public void onClick(View v) {
Almag r = new Almag();
r.setNama(nama.getText().toString());
r.setAlamat(alamat.getText().toString());
RadioGroup semester = null;
switch (semester.getCheckedRadioButtonId()) {
case R.id.ganjil:
r.setJekel(“Ganjil”);
break;
case R.id.genap:
r.setJekel(“Genap”);
break;
}
adapter.add(r);
}
};
private AdapterView.OnItemClickListener onListClick1 = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Almag r = model.get(position);
nama.setText(r.getNama());
alamat.setText(r.getAlamat());
if (r.getSemester().equals(“Ganjil”)) {
jekel.check(R.id.ganjil);
} else if (r.getJekel().equals(“Genap”)) {
jekel.check(R.id.genap);
getTabHost().setCurrentTab(1);
}
};
private View.OnClickListener onSave1 = new View.OnClickListener() {
public void onClick(View v) {
Almag r = new Almag();
r.setNama(nama.getText().toString());
r.setAlamat(alamat.getText().toString());
RadioGroup semester = null;
switch (semester.getCheckedRadioButtonId()) {
case R.id.ganjil:
r.setJekel(“Ganjil”);
break;
case R.id.genap:
r.setJekel(“Genap”);
break;
}
adapter.add(r);
}
};
private AdapterView.OnItemClickListener onListClick1 = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Almag r = model.get(position);
nama.setText(r.getNama());
alamat.setText(r.getAlamat());
if (r.getSemester().equals(“Ganjil”)) {
jekel.check(R.id.ganjil);
} else if (r.getJekel().equals(“Genap”)) {
jekel.check(R.id.genap);
}
getTabHost().setCurrentTab(1);
}
};
class AlmagAdapter extends ArrayAdapter<Almag> {
AlmagAdapter() {
super(array3.this, R.layout.row, model);
}
getTabHost().setCurrentTab(1);
}
};
class AlmagAdapter extends ArrayAdapter<Almag> {
AlmagAdapter() {
super(array3.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 nama = null;
private TextView alamat = null;
private ImageView icon = null;
private View row = null;
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 nama = null;
private TextView alamat = null;
private ImageView icon = null;
private View row = null;
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);
}
this.row = row;
nama = (TextView) row.findViewById(R.id.title);
alamat = (TextView) row.findViewById(R.id.alamat);
icon = (ImageView) row.findViewById(R.id.icon);
}
void populateFrom(Almag r) {
nama.setText(r.getNama());
alamat.setText(r.getAlamat());
if (r.getJekel().equals(“Pria”)) {
nama.setText(r.getNama());
alamat.setText(r.getAlamat());
if (r.getJekel().equals(“Pria”)) {
icon.setImageResource(R.drawable.pria);
} else if (r.getJekel().equals(“Perempuan”)) {
icon.setImageResource(R.drawable.perempuan);
}
}
}
}
Setelah itu kita akan membuat file layoutnya dimana saya membuat dua file xml dimana yang pertama saya beri nama main.xml dan tambahkan scripnya
} else if (r.getJekel().equals(“Perempuan”)) {
icon.setImageResource(R.drawable.perempuan);
}
}
}
}
Setelah itu kita akan membuat file layoutnya dimana saya membuat dua file xml dimana yang pertama saya beri nama main.xml dan tambahkan scripnya
<?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=”Nama:” />
<EditText android:id=”@+id/nama” />
</TableRow>
<TableRow>
<TextView android:text=”Jekel:” />
<RadioGroup android:id=”@+id/jekel” >
<RadioButton
android:id=”@+id/pria”
android:text=”Pria” />
<RadioButton
android:id=”@+id/perempuan”
android:text=”Perempuan” />
</RadioGroup>
</TableRow>
<TableRow>
<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=”Nama:” />
<EditText android:id=”@+id/nama” />
</TableRow>
<TableRow>
<TextView android:text=”Jekel:” />
<RadioGroup android:id=”@+id/jekel” >
<RadioButton
android:id=”@+id/pria”
android:text=”Pria” />
<RadioButton
android:id=”@+id/perempuan”
android:text=”Perempuan” />
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:text=”Semester” />
<RadioGroup android:id=”@+id/semester” >
<RadioButton
android:id=”@+id/ganjil”
android:text=”Ganjil” />
android:id=”@+id/ganjil”
android:text=”Ganjil” />
<RadioButton
android:id=”@+id/genap”
android:text=”Genap” />
</RadioGroup>
</TableRow>
android:id=”@+id/genap”
android:text=”Genap” />
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:text=”Alamat:” />
<EditText android:id=”@+id/alamat” />
</TableRow>
</TableRow>
<Button
android:id=”@+id/save”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Save” />
</TableLayout>
</FrameLayout>
</LinearLayout>
android:id=”@+id/save”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Save” />
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
kemudia untuk file xml yg kedua saya beri nama row dimana untuk scriptnya sebagai berikut
<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” >
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” />
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” >
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:ellipsize=”end”
android:gravity=”center_vertical”
android:singleLine=”true”
android:textStyle=”bold” />
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/alamat”
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/jekel”
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/semester”
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>
android:id=”@+id/alamat”
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/jekel”
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/semester”
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>
jika kita jalankan aplikasinya maka akan tampil seperti berikut
Tidak ada komentar:
Posting Komentar