Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.robot.fastboatd;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.icu.util.Calendar;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.ImageButton;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class MainActivity extends AppCompatActivity {
- public Button btn1,btn2,btn3;
- public String masukan1;
- public String masukan2;
- private DatePicker datePicker;
- private Calendar calendar;
- public String y,x,url;
- public AlertDialog.Builder builder;
- private String TAG = MainActivity.class.getSimpleName();
- private ProgressDialog pDialog;
- private ListView lv;
- ArrayList<HashMap<String, String>> dataList;
- ImageButton img;
- protected void onCreate(Bundle savedInstanceState) {
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn1 = (Button) findViewById(R.id.button1);
- btn2 = (Button) findViewById(R.id.button2);
- btn3 = (Button) findViewById(R.id.button3);
- img = (ImageButton) findViewById(R.id.imageButton);
- final CharSequence[] items = {"Lombok", "bali", "gili trawangan", "gili air", "gili meno", "nusa lembongan"};
- builder = new AlertDialog.Builder(MainActivity.this);
- btn1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- builder.setTitle("Arrive");
- builder.setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- btn1.setText(items[item]);
- masukan1 = items[item].toString();
- seturl();
- }
- }).show();
- }
- });
- btn2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- builder.setTitle("Depart");
- builder.setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- masukan2 = items[item].toString();
- btn2.setText(items[item]);
- seturl();
- }
- }).show();
- }
- });
- img.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent a = new Intent(MainActivity.this, perc.class);
- startActivity(a);
- }
- });
- btn3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- setContentView(R.layout.semogafix);
- dataList = new ArrayList<>();
- lv = (ListView) findViewById(R.id.list);
- new GetContacts().execute();
- }
- });
- }
- public class GetContacts extends AsyncTask<Void, Void, Void> {
- @Override
- public void onPreExecute() {
- super.onPreExecute();
- // Showing progress dialog
- pDialog = new ProgressDialog(MainActivity.this);
- pDialog.setMessage("Please wait...");
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected Void doInBackground(Void... arg0) {
- HttpHandler sh = new HttpHandler();
- // Making a request to url and getting response
- String jsonStr = sh.makeServiceCall(url);
- Log.e(TAG, "Response from url: " + jsonStr);
- if (jsonStr != null) {
- try {
- JSONObject jsonObj = new JSONObject(jsonStr);
- // Getting JSON Array node
- JSONArray contacts = jsonObj.getJSONArray("results");
- // looping through All Contacts
- for (int i = 0; i < contacts.length(); i++) {
- JSONObject c = contacts.getJSONObject(i);
- String boat = c.getString("boat");
- for (int y = 0; y < contacts.length(); y++) {
- JSONObject d = contacts.getJSONObject(y);
- String bot = d.getString("boat");
- if (boat == bot) {
- boat = c.getString("boat");
- String name = c.getString("agent");
- String price = c.getString("price");
- HashMap<String, String> contact = new HashMap<>();
- contact.put("boat", boat);
- contact.put("name", name);
- contact.put("price", price);
- dataList.add(contact);
- }
- }
- }
- } catch (final JSONException e) {
- Log.e(TAG, "Json parsing error: " + e.getMessage());
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- "Json parsing error: " + e.getMessage(),
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- } else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- "Koneksi Internet tidak ditemukan, silahkan aktifkan koneksi anda dan coba kembali",
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // Dismiss the progress dialog
- if (pDialog.isShowing())
- pDialog.dismiss();
- /**
- * Updating parsed JSON data into ListView
- * */
- ListAdapter adapter = new SimpleAdapter(
- MainActivity.this, dataList,
- R.layout.list_item, new String[]{"name",
- "boat","price"}, new int[]{R.id.boat,
- R.id.agent, R.id.price});
- lv.setAdapter(adapter);
- }
- }
- public void seturl(){
- url = "http://api.lrsoft.id/boat-price/v1/gets?depart="+masukan1+"&arrive="+masukan2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement