0% found this document useful (0 votes)
7 views

MainActivity - Java (ListView)

This document contains code for an Android application that displays a list of Indonesian provinces and their capitals in a ListView. It defines string arrays for the list of provinces and capitals, initializes the ListView and populates it with the province names. When a province is clicked, it displays the corresponding capital in a TextView. It also implements menu handling functions required by the Activity class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

MainActivity - Java (ListView)

This document contains code for an Android application that displays a list of Indonesian provinces and their capitals in a ListView. It defines string arrays for the list of provinces and capitals, initializes the ListView and populates it with the province names. When a province is clicked, it displays the corresponding capital in a TextView. It also implements menu handling functions required by the Activity class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 package com.alhanan.

contoh_listview;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.Menu;
6 import android.view.MenuItem;
7 import android.view.View;
8 import android.widget.AdapterView;
9 import android.widget.ArrayAdapter;
10 import android.widget.ListView;
11 import android.widget.TextView;
12
13
14 public class MainActivity extends Activity {
15 TextView textViewPropinsi, textViewIbukota, textViewNamaIbukota;
16 ListView listViewPropinsi;
17
18 String[] daftarPropinsi = {
19 "1. Aceh",
20 "2. Sumatera Utara",
21 "3. Sumatera Barat",
22 "4. Riau",
23 "5. Kepulauan Riau",
24 "6. Jambi",
25 "7. Sumatera Selatan",
26 "8. Kepulauan Bangka Belitung",
27 "9. Bengkulu",
28 "10.Lampung"
29 };
30
31 String[] daftarIbukota = {
32 "Banda Aceh",
33 "Medan",
34 "Padang",
35 "Pekanbaru",
36 "Tanjung Pinang",
37 "Jambi",
38 "Palembang",
39 "Pangkal Pinang",
40 "Bengkulu",
41 "Bandar Lampung"
42 };
43
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 setContentView(R.layout.activity_main);
48
49 listViewPropinsi = (ListView) findViewById(R.id.listViewPropinsi);
50 textViewIbukota = (TextView) findViewById(R.id.textViewIbukota);
51 textViewIbukota.setText(""); // Kosongkan
52
53 // Mengatur isi ListView
54 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
55 android.R.layout.simple_list_item_1, daftarPropinsi);
56 listViewPropinsi.setAdapter(adapter);
57
58 // Mengontrol klik pada item di ListView
59 listViewPropinsi.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
60 public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
61 String keterangan = "Propinsi " + daftarPropinsi[position] + "
beribukota di " + daftarIbukota[position];
62 textViewIbukota.setText(keterangan);
63 }
64 });
65 }
66
67 @Override
68 public boolean onCreateOptionsMenu(Menu menu) {
69 // Inflate the menu; this adds items to the action bar if it is present.
70 getMenuInflater().inflate(R.menu.main, menu);
71 return true;
72 }
73
74 @Override
75 public boolean onOptionsItemSelected(MenuItem item) {
76 // Handle action bar item clicks here. The action bar will
77 // automatically handle clicks on the Home/Up button, so long
78 // as you specify a parent activity in AndroidManifest.xml.
79 int id = item.getItemId();
80 if (id == R.id.action_settings) {
81 return true;
82 }
83 return super.onOptionsItemSelected(item);
84 }
85 }
86

You might also like