Assalamualaikum,
untuk tugas UAS kali ini tentang aplikasi yang menggunakan listview. Monggo diliat sendiri script codingnya ;)
1.
Skenario-nya, terdapat 4 activity. Pada activity pertama terdapat 3 buah list
yang jika di-klik akan terhubung dengan 3 activity lainnya
2.
Buka dan edit file (dari package explorer) src >
dlmbg.pckg.tes.intent > TesIntentActivity.java.
1
|
public class TesIntentActivity extends ListActivity {
|
|
2
|
String[]
perangkat = { "Activity Laptop", "Activity
Komputer", "Activity Smartphone"};
|
3
|
|
|
4
|
@Override
|
5
|
public void onCreate(Bundle
savedInstanceState) {
|
|
6
|
super.onCreate(savedInstanceState);
|
7
|
setContentView(R.layout.main);
|
||
8
|
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
perangkat));
|
||
9
|
}
|
||
10
|
|
||
11
|
public void onListItemClick(ListView
parent, View v, int urutan, long id) {
|
|
12
|
Object
detail = this.getListAdapter().getItem(urutan);
|
13
|
String
tampil = detail.toString();
|
|
14
|
Intent
i = null;
|
15
|
if(tampil=="Activity
Laptop")
|
|
16
|
{
|
17
|
i
= new Intent(TesIntentActivity.this,
ActivityLaptop.class);
|
|
18
|
startActivity(i);
|
19
|
}
|
|
20
|
else if(tampil=="Activity
Komputer")
|
21
|
{
|
|
22
|
i
= new Intent(TesIntentActivity.this,
ActivityKomputer.class);
|
23
|
startActivity(i);
|
|
24
|
}
|
25
|
else if(tampil=="Activity
Smartphone")
|
|
26
|
{
|
27
|
i
= new Intent(TesIntentActivity.this,
ActivitySmartphone.class);
|
|
28
|
startActivity(i);
|
29
|
}
|
|
30
|
}
|
31
|
}
|
3. Sekarang buka dan edit file layout, res
> layout > main.xml.
1
|
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
<LinearLayout
|
3
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
|
android:orientation="vertical"
|
5
|
android:layout_width="fill_parent"
|
|
6
|
android:layout_height="fill_parent" >
|
7
|
<ListView
|
|
8
|
android:id="@android:id/list"
|
9
|
android:layout_width="fill_parent"
|
||
10
|
android:layout_height="fill_parent"
|
||
11
|
android:drawSelectorOnTop="false"
|
|
12
|
/>
|
13
|
</LinearLayout>
|
4. Karena akan terhubung dengan 3
activity yang menampilkan data yang berbeda, kita buat terlbih dahulu ketiga
activity tersebut yaitu, ActivityKomputer, ActivityLaptop, dan ActivitySmartphone.
ActivityKomputer.java
1
|
public class ActivityKomputer extends ListActivity {
|
|
2
|
String[]
perangkat = { "Activity IBM", "Activity
Lenovo", "Activity HP","Activity Dell", "<<
Kembali"};
|
3
|
|
|
4
|
@Override
|
5
|
public void onCreate(Bundle
savedInstanceState) {
|
|
6
|
super.onCreate(savedInstanceState);
|
7
|
setContentView(R.layout.komputer);
|
||
8
|
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
perangkat));
|
||
9
|
}
|
||
10
|
|
||
11
|
public void onListItemClick(ListView
parent, View v, int urutan, long id) {
|
|
12
|
Object
detail = this.getListAdapter().getItem(urutan);
|
13
|
String
tampil = detail.toString();
|
|
14
|
Intent
i = null;
|
15
|
if(tampil=="<<
Kembali")
|
|
16
|
{
|
17
|
i
= new Intent(ActivityKomputer.this,
TesIntentActivity.class);
|
|
18
|
startActivity(i);
|
19
|
}
|
|
20
|
else
|
21
|
{
|
|
22
|
Toast.makeText(this, "Anda
Memilih : " + tampil,
Toast.LENGTH_SHORT).show();
|
23
|
}
|
|
24
|
}
|
25
|
}
|
ActivityLaptop.java
1
|
public class ActivityLaptop extends ListActivity {
|
|
2
|
String[]
perangkat = { "Activity Asus",
|
3
|
"Activity
Acer",
|
|
4
|
"Activity
Toshiba",
|
5
|
"Activity
Sony",
|
|
6
|
"Activity
Axioo",
|
7
|
"Activity
Dell",
|
|
8
|
"<<
Kembali"};
|
9
|
|
||
10
|
@Override
|
||
11
|
public void onCreate(Bundle
savedInstanceState) {
|
|
12
|
super.onCreate(savedInstanceState);
|
13
|
setContentView(R.layout.laptop);
|
||
14
|
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
perangkat));
|
||
15
|
}
|
|
16
|
|
17
|
public void onListItemClick(ListView
parent, View v, int urutan, long id) {
|
|
18
|
Object
detail = this.getListAdapter().getItem(urutan);
|
19
|
String
tampil = detail.toString();
|
|
20
|
Intent
i = null;
|
21
|
if(tampil=="<<
Kembali")
|
|
22
|
{
|
23
|
i
= new Intent(ActivityLaptop.this,
TesIntentActivity.class);
|
|
24
|
startActivity(i);
|
25
|
}
|
|
26
|
else
|
27
|
{
|
|
28
|
Toast.makeText(this, "Anda
Memilih : " + tampil,
Toast.LENGTH_SHORT).show();
|
29
|
}
|
|
30
|
}
|
31
|
}
|
ActivitySmartphone.java
1
|
public class ActivitySmartphone extends ListActivity {
|
|
2
|
String[]
perangkat = { "Activity Sony Ericsson",
|
3
|
"Activity
Samsung",
|
|
4
|
"Activity
HTC",
|
5
|
"Activity
Apple",
|
|
6
|
"Activity
Blackberry", "<< Kembali"};
|
7
|
|
|
8
|
@Override
|
9
|
public void onCreate(Bundle
savedInstanceState) {
|
||
10
|
super.onCreate(savedInstanceState);
|
||
11
|
setContentView(R.layout.smartphone);
|
||
12
|
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
perangkat));
|
||
13
|
}
|
|
14
|
|
15
|
public void onListItemClick(ListView
parent, View v, int urutan, long id) {
|
|
16
|
Object
detail = this.getListAdapter().getItem(urutan);
|
17
|
String
tampil = detail.toString();
|
|
18
|
Intent
i = null;
|
19
|
if(tampil=="<<
Kembali")
|
|
20
|
{
|
21
|
i
= new Intent(ActivitySmartphone.this,
TesIntentActivity.class);
|
|
22
|
startActivity(i);
|
23
|
}
|
|
24
|
else
|
25
|
{
|
|
26
|
Toast.makeText(this, "Anda
Memilih : " + tampil,
Toast.LENGTH_SHORT).show();
|
27
|
}
|
|
28
|
}
|
29
|
}
|
5. Kita lanjutkan dengan membuat layout
dari masing-masing ketiga activity di atas. Sebenarnya layout yang digunakan
sama saja, sama-sama hanya memanggil Listview. Tapi agar terbiasa
bekerja rapi dan terstruktur, kita pisah saja layout-nya.
komputer.xml
1
|
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
<LinearLayout
|
3
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
|
android:orientation="vertical"
|
5
|
android:layout_width="fill_parent"
|
|
6
|
android:layout_height="fill_parent" >
|
7
|
<ListView
|
|
8
|
android:id="@android:id/list"
|
9
|
android:layout_width="fill_parent"
|
||
10
|
android:layout_height="fill_parent"
|
||
11
|
android:drawSelectorOnTop="false"
|
|
12
|
/>
|
13
|
</LinearLayout>
|
laptop.xml
1
|
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
<LinearLayout
|
3
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
|
android:orientation="vertical"
|
5
|
android:layout_width="fill_parent"
|
|
6
|
android:layout_height="fill_parent" >
|
7
|
<ListView
|
|
8
|
android:id="@android:id/list"
|
9
|
android:layout_width="fill_parent"
|
||
10
|
android:layout_height="fill_parent"
|
||
11
|
android:drawSelectorOnTop="false"
|
|
12
|
/>
|
13
|
</LinearLayout>
|
smartphone.xml
1
|
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
<LinearLayout
|
3
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
4
|
android:orientation="vertical"
|
5
|
android:layout_width="fill_parent"
|
|
6
|
android:layout_height="fill_parent" >
|
7
|
<ListView
|
|
8
|
android:id="@android:id/list"
|
9
|
android:layout_width="fill_parent"
|
||
10
|
android:layout_height="fill_parent"
|
||
11
|
android:drawSelectorOnTop="false"
|
|
12
|
/>
|
13
|
</LinearLayout>
|
6. Jangan lupa untuk mendaftarkan ketiga
activity ke dalam file AndroidManifest.xml. Kalau gak
didaftarkan, saya jamin aplikasinya akan force close :D, minimal activity-nya
gak bisa dipanggil.
1
|
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
3
|
package="dlmbg.pckg.tes.intent"
|
|
4
|
android:versionCode="1"
|
5
|
android:versionName="1.0">
|
|
6
|
<uses-sdk
android:minSdkVersion="10" />
|
7
|
|
|
8
|
<application
android:icon="@drawable/icon" android:label="@string/app_name">
|
9
|
<activity
android:name=".TesIntentActivity"
|
||
10
|
android:label="Berpindah
Activity Dengan Intent">
|
||
11
|
<intent-filter>
|
|
12
|
<action
android:name="android.intent.action.MAIN" />
|
13
|
<category
android:name="android.intent.category.LAUNCHER" />
|
|
14
|
</intent-filter>
|
15
|
</activity>
|
|
16
|
|
17
|
<activity
android:name="ActivityLaptop" android:label="Activity
Laptop"></activity>
|
18
|
<activity
android:name="ActivityKomputer" android:label="Activity
Komputer"></activity>
|
19
|
<activity
android:name="ActivitySmartphone" android:label="Activity
Smartphone"></activity>
|
|
20
|
</application>
|
21
|
</manifest>
|
7.
Jalankan aplikasi melalui emulator (tekan Ctrl + F11). Kira-kira hasilnya
seperti di bawah ini :
Perintah
untuk berpindah ke activity lain terbilang sederhana, tinggal mendeklarasikan
ulang object Intent dan menggunakan format seperti di bawah ini.
1
|
Intent i = null;
|
|
2
|
i = new Intent(ActivityPertama.this,
ActivityKedua.class);
|
3
|
startActivity(i);
|
OK
deh, berhubung listrik di rumah lagi mati dan si laptop baterainya sudah mau
habis, saya akhiri postingan tentang android kali ini. Semoga bisa bermanfaat
untuk rekan-rekan yang lagi belajar pemrograman di platform android