Tutorial ini lanjutan dari tutorial sebelumnya yaitu Android PHP MySQL. Pada tutorial ini kita akan menampilkan listview dari database MySQL yang apabila di klik maka akan menampilkan activity detail dari data tersebut.
Langsung saja kita praktekkan :
Pertama : kita buat database dan bisa menggunakan database yang telah ada pada tutorial sebelumnya
Kedua : kita buat file daftar-wisata.php, detail-wisata.php dan koneksi.php
daftar-wisata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| <?php include( "koneksi.php" ); $q = mysql_query( 'SELECT * FROM daftar' ); $v = '{"wisata" : [' ; while ($r=mysql_fetch_array($q)) { $ob = array( '"' ,' ',' </br>'); if (strlen($v)< 15 ) { $v .= '{"id" : "' .str_replace($ob, ' ' ,strip_tags($r["id_wisata "])).'" , "nama" : "'.str_replace($ob,' ',strip_tags($r[" nama_wisata "])).'" , "deskripsi" : "'.str_replace($ob,' ',strip_tags($r[" deskripsi "])).'" , "latitude" : "'.str_replace($ob,' ',strip_tags($r[" latitude "])).'" , "longitude" : "'.str_replace($ob,' ',strip_tags($r[" longitude "])).'" , "gambar" : "/'.str_replace($ob,' ',strip_tags($r[" gambar "])).'" }'; } else { $v .= ',{"id" : "' .str_replace($ob, ' ' ,strip_tags($r["id_wisata "])).'" , "nama" : "'.str_replace($ob,' ',strip_tags($r[" nama_wisata "])).'" , "deskripsi" : "'.str_replace($ob,' ',strip_tags($r[" deskripsi "])).'" , "latitude" : "'.str_replace($ob,' ',strip_tags($r[" latitude "])).'" , "longitude" : "'.str_replace($ob,' ',strip_tags($r[" longitude "])).'" , "gambar" : "/'.str_replace($ob,' ',strip_tags($r[" gambar "])).'" }'; } } $v .= ']}' ; echo $v; ?> |
detail-wisata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?php $kd = $_GET[ 'kode' ]; include( "koneksi.php" ); $q = mysql_query( 'SELECT * FROM daftar WHERE id_wisata="' .$kd. '"' ); $v = '{"wisata" : [' ; while ($r=mysql_fetch_array($q)) { $ob = array( '"' ,' ',' </br>'); $v .= '{"id" : "' .str_replace($ob, ' ' ,strip_tags($r["id_wisata "])).'" , "nama" : "'.str_replace($ob,' ',strip_tags($r[" nama_wisata "])).'" , "deskripsi" : "'.str_replace($ob,' ',strip_tags($r[" deskripsi "])).'" , "latitude" : "'.str_replace($ob,' ',strip_tags($r[" latitude "])).'" , "longitude" : "'.str_replace($ob,' ',strip_tags($r[" longitude "])).'" , "gambar" : "'.str_replace($ob,' ',strip_tags($r[" gambar "])).'" }'; } $v .= ']}' ; echo $v; ?> |
koneksi.php
1
2
3
4
5
| <?php //Koneksi ke database $conn = mysql_connect( "localhost" , "root" , "" ); $db = mysql_select_db( "db_wisatasulsel" ); ?> |
Ketiga : buat project androidnya
pada bagian layout terdapat tiga file yaitu main.xml, list_item.xml, single_list_item.xml
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
| <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <ListView android:id= "@android:id/list" android:layout_width= "fill_parent" android:layout_height= "wrap_content" /> </LinearLayout> |
list_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:layout_gravity= "right" android:orientation= "vertical" android:padding= "7dp" > <RelativeLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" > <!-- Name Label --> <TextView android:id= "@+id/nama" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:paddingBottom= "2dip" android:textColor= "#333333" android:textSize= "16sp" android:textStyle= "bold" /> <ImageView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentRight= "true" android:layout_centerVertical= "true" android:src= "@drawable/arrow" /> <TextView android:id= "@+id/kode" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:textColor= "#D7DEF0" android:textSize= "1sp" > </TextView> </RelativeLayout> </LinearLayout> |
single_list_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#fff" android:orientation= "vertical" android:padding= "10dp" > <ImageView android:id= "@+id/gambar" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:src= "@drawable/no_image" /> <TextView android:id= "@+id/nama_wisata" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:paddingBottom= "5dip" android:paddingTop= "10dip" android:textColor= "#333333" android:textSize= "20dip" android:textStyle= "bold" /> <TextView android:id= "@+id/latitude" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:paddingBottom= "5dip" android:paddingTop= "5dip" android:textColor= "#999999" android:textSize= "12dip" android:textStyle= "bold" /> <TextView android:id= "@+id/longitude" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:paddingBottom= "5dip" android:paddingTop= "5dip" android:textColor= "#999999" android:textSize= "12dip" android:textStyle= "bold" /> <ScrollView android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TextView android:id= "@+id/deskripsi" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textColor= "#333333" android:textSize= "14dip" /> </ScrollView> </LinearLayout> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| public class MainActivity extends ListActivity { private static final String AR_ID = "id" ; private static final String AR_JUDUL = "nama" ; JSONArray wisata = null ; ArrayList<HashMap<String, String>> daftar_wisata = new ArrayList<HashMap<String, String>>(); @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); JSONParser jParser = new JSONParser(); JSONObject json = jParser.AmbilJson(link_url); try { wisata = json.getJSONArray( "wisata" ); for ( int i = 0 ; i < wisata.length(); i++){ JSONObject ar = wisata.getJSONObject(i); String id = ar.getString(AR_ID); String nama = ar.getString(AR_JUDUL); HashMap<String, String> map = new HashMap<String, String>(); map.put(AR_ID, id); map.put(AR_JUDUL, nama); daftar_wisata.add(map); } } catch (JSONException e) { e.printStackTrace(); } this .adapter_listview(); } public void adapter_listview() { ListAdapter adapter = new SimpleAdapter( this , daftar_wisata, R.layout.list_item, new String[] { AR_JUDUL, AR_ID}, new int [] { R.id.nama, R.id.kode}); setListAdapter(adapter); ListView lv = getListView(); lv.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String kode = ((TextView) view.findViewById(R.id.kode)).getText().toString(); Intent in = new Intent(MainActivity. this , Detail. class ); in.putExtra(AR_ID, kode); startActivity(in); } }); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| public class Detail extends Activity { private static final String AR_ID = "id" ; JSONArray wisata = null ; String gambar; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.single_list_item); Intent in = getIntent(); String kode = in.getStringExtra(AR_ID); JSONParser jParser = new JSONParser(); JSONObject json = jParser.AmbilJson(link_url); try { wisata = json.getJSONArray( "wisata" ); for ( int i = 0 ; i < wisata.length(); i++){ JSONObject ar = wisata.getJSONObject(i); TextView nama = (TextView) findViewById(R.id.nama_wisata); TextView lat = (TextView) findViewById(R.id.latitude); TextView lon = (TextView) findViewById(R.id.longitude); TextView desk = (TextView) findViewById(R.id.deskripsi); String nama_d = ar.getString( "nama" ); String lat_d = ar.getString( "latitude" ); String lon_d = ar.getString( "longitude" ); String desk_d = ar.getString( "deskripsi" ); nama.setText(nama_d); lat.setText( "Latitude : " +lat_d); lon.setText( "Longitude : " +lon_d); desk.setText( "Deskripsi : " + "\n" + desk_d); } catch (JSONException e){ e.printStackTrace(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| public class JSONParser { static InputStream is = null ; static JSONObject jObj = null ; static String json = "" ; public JSONObject AmbilJson(String url) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader( new InputStreamReader(is, "iso-8859-1" ), 8 ); StringBuilder sb = new StringBuilder(); String line = null ; while ((line = reader.readLine()) != null ) { sb.append(line); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e( "Buffer Error" , "Error converting result " + e.toString()); } try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e( "JSON Parser" , "Error parsing data " + e.toString()); } return jObj; } } |
1
2
| <uses-permission android:name= "android.permission.INTERNET" > </uses-permission> |
Post a Comment