Advertisement
rachmadi

Cari Lapangan

Jun 4th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.15 KB | None | 0 0
  1. package com.futsal.skripsi.futsal;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.Log;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.ImageView;
  13. import android.widget.ListView;
  14. import android.widget.Toast;
  15.  
  16. import com.android.volley.Request;
  17. import com.android.volley.Response;
  18. import com.android.volley.VolleyError;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  20. import com.google.android.gms.common.api.GoogleApiClient;
  21. import com.squareup.picasso.Picasso;
  22.  
  23. import org.json.JSONArray;
  24. import org.json.JSONObject;
  25.  
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30.  
  31. public class PilihTempat extends AppCompatActivity {
  32.     // Log tag
  33.     private static final String TAG = PilihTempat.class.getSimpleName();
  34.  
  35.     int sukses;
  36.  
  37.     String url = "carilapangan.php";
  38.  
  39.     private ProgressDialog pDialog;
  40.     private List<Lapangan> lapanganList = new ArrayList<Lapangan>();
  41.     private List<Lapangan> lapanganList2 = new ArrayList<Lapangan>();
  42.     private ListView listView;
  43.     private CustomListAdapter adapter;
  44.  
  45.     Utilities util = new Utilities();
  46.     String tanggal, dari, durasi, hari, jam, harga;
  47.     /**
  48.      * ATTENTION: This was auto-generated to implement the App Indexing API.
  49.      * See https://g.co/AppIndexing/AndroidStudio for more information.
  50.      */
  51.     private GoogleApiClient client;
  52.  
  53.     @Override
  54.     protected void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.activity_pilih_tempat);
  57.  
  58.         url = util.getUrlPhp(url);
  59.  
  60.         Bundle extras = getIntent().getExtras();
  61.         tanggal = extras.getString("tgl_main");
  62.         dari = extras.getString("jam_mulai");
  63.         durasi = extras.getString("durasi");
  64.         hari = extras.getString("id_hari");
  65.         jam = extras.getString("id_jam");
  66.  
  67.         pDialog = new ProgressDialog(this);
  68. //         Showing progress dialog before making http request
  69.         pDialog.setMessage("Loading...");
  70.         pDialog.show();
  71.  
  72.         lapangan();
  73.  
  74.         listView = (ListView) findViewById(R.id.list);
  75.         adapter = new CustomListAdapter(this, lapanganList);
  76.         listView.setAdapter(adapter);
  77.  
  78.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  79.             @Override
  80.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  81.                 Lapangan lapangan = new Lapangan();
  82.                 lapangan = lapanganList.get(position);
  83.                 String idl = lapangan.getIdLapangan();
  84.                 for (int i = 0; i < lapanganList.size(); i++) {
  85.                     Lapangan lap = lapanganList.get(i);
  86.                     String idlap = lap.getIdLapangan();
  87.                     if (idlap.equals(idl)) {
  88.                         System.out.println("idl: " + idl + ", " + "idlap: " + idlap);
  89.                         String gambar = lap.getThumbnailUrl();
  90.                         String nama = lap.getNama();
  91.                         String idLokasi = lapangan.getIdLokasi();
  92.                         String idHari = lap.getIdHari();
  93.                         String idJam = lap.getIdJam();
  94.                         String latitude = lapangan.getLat();
  95.                         String longitude = lapangan.getLng();
  96.                         String harga = lap.getHarga();
  97.                         System.out.println("nama: " + nama + ", " + "idjam: " + idJam + ", " + "harga: " + harga);
  98.  
  99. //                        Intent intent = new Intent(getApplicationContext(), DetailTempat.class);
  100. //                        intent.putExtra("nama", nama);
  101. //                        intent.putExtra("gambar", gambar);
  102. //                        intent.putExtra("idLokasi", idLokasi);
  103. //                        intent.putExtra("idLapangan", idl);
  104. //                        intent.putExtra("idHari", idHari);
  105. //                        intent.putExtra("idJam", idJam);
  106. //                        intent.putExtra("latitude", latitude);
  107. //                        intent.putExtra("longitude", longitude);
  108. //                        intent.putExtra("harga", harga);
  109. //                        intent.putExtra("durasi", durasi);
  110. //                        intent.putExtra("tgl_main", tanggal);
  111. //                        intent.putExtra("jam_mulai", dari);
  112. //                        startActivity(intent);
  113.                     }
  114.                 }
  115. //                String gambar = lapangan.getThumbnailUrl();
  116. //                String nama = lapangan.getNama();
  117. //                String idl = lapangan.getId();
  118. //                String idHari = lapangan.getIdHari();
  119. //                String idJam = lapangan.getIdJam();
  120. //                String latitude = lapangan.getLat();
  121. //                String longitude = lapangan.getLng();
  122. //                String harga = lapangan.getHarga();
  123.  
  124. //                System.out.println("nama lap: " + nama);
  125.  
  126. //                Intent intent = new Intent(getApplicationContext(), DetailTempat.class);
  127. //                intent.putExtra("nama", nama);
  128. //                intent.putExtra("gambar", gambar);
  129. //                intent.putExtra("id", idl);
  130. //                intent.putExtra("idHari", idHari);
  131. //                intent.putExtra("idJam", idJam);
  132. //                intent.putExtra("latitude", latitude);
  133. //                intent.putExtra("longitude", longitude);
  134. //                intent.putExtra("harga", harga);
  135. //                intent.putExtra("durasi", durasi);
  136. //                intent.putExtra("tgl_main", tanggal);
  137. //                intent.putExtra("jam_mulai", dari);
  138. //                startActivity(intent);
  139.             }
  140.         });
  141.     }
  142.  
  143.     @Override
  144.     public void onDestroy() {
  145.         super.onDestroy();
  146.         hidePDialog();
  147.     }
  148.  
  149.     private void hidePDialog() {
  150.         if (pDialog != null) {
  151.             pDialog.dismiss();
  152.             pDialog = null;
  153.         }
  154.     }
  155.  
  156.     private void lapangan() {
  157.  
  158.         Map<String, String> params = new HashMap<String, String>();
  159.         params.put("tgl_main", tanggal);
  160.         params.put("jam_mulai", dari);
  161.         params.put("durasi", durasi);
  162.         System.out.println("Params: " + tanggal + "," + dari + "," + durasi);
  163.         System.out.println("Requesting...");
  164.         CustomRequest jsonObj = new CustomRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
  165.  
  166.             @Override
  167.             public void onResponse(JSONObject response) {
  168.                 try {
  169.                     Log.d("Response: ", response.toString());
  170.                     sukses = response.getInt("sukses");
  171.                     System.out.println("Sukses: " + sukses);
  172.                     if (sukses == 1) {
  173.                         JSONArray lokasi = response.getJSONArray("lapangan");
  174.                         for (int i = 0; i < lokasi.length(); i++) {
  175.                             JSONObject l = (JSONObject) lokasi.get(i);
  176.                             Lapangan lapangan = new Lapangan();
  177.                             String nama = l.getString("nama_lapangan");
  178.                             String alamat = l.getString("alamat_futsal");
  179.                             String gambar = l.getString("gambar");
  180.                             String gbr = util.getUrl() + gambar;
  181.                             String idl = l.getString("id_lapangan");
  182.                             String idLokasi = l.getString("id_lokasi");
  183. //                            String idHari = l.getString("id_hari");
  184.                             String idJam = l.getString("id_jam");
  185.                             String lat = l.getString("latitude");
  186.                             String lng = l.getString("longitude");
  187.                             String harga = l.getString("harga");
  188.  
  189.                             lapangan.setNama(nama);
  190.                             lapangan.setAlamat(alamat);
  191.                             lapangan.setThumbnailUrl(gbr);
  192.                             lapangan.setId_lapangan(idl);
  193.                             lapangan.setId_lokasi(idLokasi);
  194. //                            lapangan.setIdHari(idHari);
  195.                             lapangan.setIdJam(idJam);
  196.                             lapangan.setLat(lat);
  197.                             lapangan.setLng(lng);
  198.                             lapangan.setHarga(harga);
  199.  
  200.                             lapanganList.add(lapangan);
  201.                         }
  202.                         // untuk cek
  203. //                        for (int j = 0; j < lapanganList.size(); j++) {
  204. //                            Lapangan lp = new Lapangan();
  205. //                            lp = lapanganList.get(j);
  206. //                            String namal = lp.getNama();
  207. //                            String alamatl = lp.getAlamat();
  208. //                            String gambarl = lp.getThumbnailUrl();
  209. //                            String idl = lp.getId();
  210. //                            System.out.println("lapanganList: " + j + gambarl + "," + namal + "," + alamatl + "," + idl);
  211. //                        }
  212.                         fillLapangan();
  213.                         pDialog.hide();
  214.                     } else {
  215.                         Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
  216.                         pDialog.hide();
  217.                     }
  218.  
  219.                 } catch (Exception e) {
  220.                     // TODO Auto-generated catch block
  221.                     e.printStackTrace();
  222.                     pDialog.hide();
  223.                     Toast.makeText(getApplicationContext(), "Error!" + e.toString(), Toast.LENGTH_LONG).show();
  224.                 }
  225.             }
  226.         }, new Response.ErrorListener() {
  227.             @Override
  228.             public void onErrorResponse(VolleyError response) {
  229.                 Log.d("Response: ", response.toString());
  230.             }
  231.         });
  232.         AppController.getInstance().addToRequestQueue(jsonObj);
  233.     }
  234.  
  235.     public void fillLapangan() {
  236.         String idx = null;
  237.         for (int i = 0; i < lapanganList.size(); i++){
  238.             Lapangan lapangan2 = new Lapangan();
  239.             Lapangan lap = lapanganList.get(i);
  240.             String id = lap.getIdLapangan();
  241.             System.out.println("id: " + id);
  242.             if (id.equals(idx)){
  243.                 System.out.println("id = idx");
  244.                 continue;
  245.             } else {
  246.                 idx = id;
  247.                 String nama = lap.getNama();
  248.                 String alamat = lap.getAlamat();
  249.                 String gambar = lap.getThumbnailUrl();
  250.                 String harga = lap.getHarga();
  251.                 System.out.println("id: " + id + ", " + "idx: " + idx + ", " + "nama: " + nama + ", " + "alamat: " + alamat);
  252.                 lapangan2.setNama(nama);
  253.                 lapangan2.setAlamat(alamat);
  254.                 lapangan2.setThumbnailUrl(gambar);
  255.                 lapangan2.setHarga(harga);
  256.                 lapanganList2.add(lapangan2);
  257.             }
  258.         }
  259.         adapter = new CustomListAdapter(this, lapanganList2);
  260.         listView.setAdapter(adapter);
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement