Advertisement
zhack000

MainActivity

Aug 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1. package com.example.robot.fastboatd;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.icu.util.Calendar;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AlertDialog;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.view.Window;
  14. import android.widget.Button;
  15. import android.widget.DatePicker;
  16. import android.widget.ImageButton;
  17. import android.widget.ListAdapter;
  18. import android.widget.ListView;
  19. import android.widget.SimpleAdapter;
  20. import android.widget.Toast;
  21.  
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31.     public Button btn1,btn2,btn3;
  32.     public String masukan1;
  33.     public String masukan2;
  34.     private DatePicker datePicker;
  35.     private Calendar calendar;
  36.     public String y,x,url;
  37.     public AlertDialog.Builder builder;
  38.  
  39.     private String TAG = MainActivity.class.getSimpleName();
  40.  
  41.     private ProgressDialog pDialog;
  42.     private ListView lv;
  43.     ArrayList<HashMap<String, String>> dataList;
  44.  
  45.     ImageButton img;
  46.     protected void onCreate(Bundle savedInstanceState) {
  47.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  48.         super.onCreate(savedInstanceState);
  49.         setContentView(R.layout.activity_main);
  50.         btn1 = (Button) findViewById(R.id.button1);
  51.         btn2 = (Button) findViewById(R.id.button2);
  52.         btn3 = (Button) findViewById(R.id.button3);
  53.         img = (ImageButton) findViewById(R.id.imageButton);
  54.         final CharSequence[] items = {"Lombok", "bali", "gili trawangan", "gili air", "gili meno", "nusa lembongan"};
  55.         builder = new AlertDialog.Builder(MainActivity.this);
  56.         btn1.setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View v) {
  59.                         builder.setTitle("Arrive");
  60.                         builder.setItems(items, new DialogInterface.OnClickListener() {
  61.                             public void onClick(DialogInterface dialog, int item) {
  62.                                 btn1.setText(items[item]);
  63.                                 masukan1 = items[item].toString();
  64.                                 seturl();
  65.                             }
  66.                         }).show();
  67.             }
  68.         });
  69.         btn2.setOnClickListener(new View.OnClickListener() {
  70.             @Override
  71.             public void onClick(View v) {
  72.                 builder.setTitle("Depart");
  73.                 builder.setItems(items, new DialogInterface.OnClickListener() {
  74.                     public void onClick(DialogInterface dialog, int item) {
  75.                         masukan2 = items[item].toString();
  76.                         btn2.setText(items[item]);
  77.                         seturl();
  78.                     }
  79.                 }).show();
  80.             }
  81.         });
  82.  
  83.         img.setOnClickListener(new View.OnClickListener() {
  84.             @Override
  85.             public void onClick(View v) {
  86.                 Intent a = new Intent(MainActivity.this, perc.class);
  87.                 startActivity(a);
  88.             }
  89.         });
  90.         btn3.setOnClickListener(new View.OnClickListener() {
  91.             @Override
  92.             public void onClick(View v) {
  93.                 setContentView(R.layout.semogafix);
  94.                 dataList = new ArrayList<>();
  95.  
  96.                 lv = (ListView) findViewById(R.id.list);
  97.  
  98.  
  99.                 new GetContacts().execute();
  100.             }
  101.         });
  102.  
  103.     }
  104.     public class GetContacts extends AsyncTask<Void, Void, Void> {
  105.  
  106.  
  107.         @Override
  108.         public void onPreExecute() {
  109.             super.onPreExecute();
  110.             // Showing progress dialog
  111.             pDialog = new ProgressDialog(MainActivity.this);
  112.             pDialog.setMessage("Please wait...");
  113.             pDialog.setCancelable(true);
  114.  
  115.             pDialog.show();
  116.  
  117.         }
  118.  
  119.         @Override
  120.         protected Void doInBackground(Void... arg0) {
  121.             HttpHandler sh = new HttpHandler();
  122.  
  123.             // Making a request to url and getting response
  124.             String jsonStr = sh.makeServiceCall(url);
  125.  
  126.             Log.e(TAG, "Response from url: " + jsonStr);
  127.  
  128.             if (jsonStr != null) {
  129.                 try {
  130.                     JSONObject jsonObj = new JSONObject(jsonStr);
  131.  
  132.                     // Getting JSON Array node
  133.                     JSONArray contacts = jsonObj.getJSONArray("results");
  134.  
  135.                     // looping through All Contacts
  136.                     for (int i = 0; i < contacts.length(); i++) {
  137.                         JSONObject c = contacts.getJSONObject(i);
  138.                         String boat = c.getString("boat");
  139.                         for (int y = 0; y < contacts.length(); y++) {
  140.                             JSONObject d = contacts.getJSONObject(y);
  141.                             String bot = d.getString("boat");
  142.                             if (boat == bot) {
  143.                                 boat = c.getString("boat");
  144.                                 String name = c.getString("agent");
  145.                                 String price = c.getString("price");
  146.  
  147.  
  148.                                 HashMap<String, String> contact = new HashMap<>();
  149.  
  150.                                 contact.put("boat", boat);
  151.                                 contact.put("name", name);
  152.                                 contact.put("price", price);
  153.  
  154.                                 dataList.add(contact);
  155.                             }
  156.                         }
  157.                     }
  158.  
  159.                 } catch (final JSONException e) {
  160.                     Log.e(TAG, "Json parsing error: " + e.getMessage());
  161.                     runOnUiThread(new Runnable() {
  162.                         @Override
  163.                         public void run() {
  164.                             Toast.makeText(getApplicationContext(),
  165.                                     "Json parsing error: " + e.getMessage(),
  166.                                     Toast.LENGTH_LONG)
  167.                                     .show();
  168.                         }
  169.                     });
  170.  
  171.                 }
  172.             } else {
  173.                 runOnUiThread(new Runnable() {
  174.                     @Override
  175.                     public void run() {
  176.                         Toast.makeText(getApplicationContext(),
  177.                                 "Koneksi Internet tidak ditemukan, silahkan aktifkan koneksi anda dan coba kembali",
  178.                                 Toast.LENGTH_LONG)
  179.                                 .show();
  180.                     }
  181.                 });
  182.  
  183.             }
  184.  
  185.             return null;
  186.         }
  187.  
  188.         @Override
  189.         protected void onPostExecute(Void result) {
  190.             super.onPostExecute(result);
  191.             // Dismiss the progress dialog
  192.             if (pDialog.isShowing())
  193.                 pDialog.dismiss();
  194.             /**
  195.              * Updating parsed JSON data into ListView
  196.              * */
  197.             ListAdapter adapter = new SimpleAdapter(
  198.                     MainActivity.this, dataList,
  199.                     R.layout.list_item, new String[]{"name",
  200.                     "boat","price"}, new int[]{R.id.boat,
  201.                     R.id.agent, R.id.price});
  202.  
  203.             lv.setAdapter(adapter);
  204.         }
  205.  
  206.     }
  207.     public void seturl(){
  208.         url = "http://api.lrsoft.id/boat-price/v1/gets?depart="+masukan1+"&arrive="+masukan2;
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement