Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JSONArray tambalban = null;
- ArrayList<HashMap<String, String>> listMotor = new ArrayList<HashMap<String,String>>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- new GetJSON().execute();
- }
- public class GetJSON extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>>{
- @Override
- protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
- // TODO Auto-generated method stub
- JSONParser jParser = new JSONParser();
- JSONObject json = jParser.getJSONFromURL(url);
- try {
- tambalban = json.getJSONArray(TAG_MOTOR);
- for (int i = 0; i < tambalban.length(); i++){
- JSONObject mtr = tambalban.getJSONObject(i);
- String id_tb = mtr.getString(TAG_ID);
- String name_tb = mtr.getString(TAG_NAME);
- String address_tb = mtr.getString(TAG_ADDRESS);
- String telp_tb = mtr.getString(TAG_TELP);
- String lat = mtr.getString(TAG_LAT);
- String lng = mtr.getString(TAG_LNG);
- double lat_tujuan = Double.parseDouble(lat);
- double lng_tujuan = Double.parseDouble(lng);
- double lat_asal = -7.781083333;
- double lng_asal = 110.4824833;
- double distance = hitungJarak (lat_asal, lng_asal, lat_tujuan, lng_tujuan);
- distance = RoundDecimal(distance, 2);
- String jarak = Double.toString(distance)+ " km";
- HashMap<String, String> a = new HashMap<String, String>();
- a.put(TAG_ID, id_tb);
- a.put(TAG_NAME, name_tb);
- a.put(TAG_ADDRESS, address_tb);
- a.put(TAG_TELP, telp_tb);
- a.put(TAG_LAT, lat);
- a.put(TAG_LNG, lng);
- a.put(TAG_DISTANCE, distance);
- listMotor.add(a);
- **Sorting Code**
- Collections.sort(listMotor, new Comparator<HashMap<String, String>>() {
- @Override
- public int compare(HashMap<String, String> o1,
- HashMap<String, String> o2) {
- return Double.compare(o1.get(Double.parseDouble(TAG_DISTANCE)), o1.get(Double.parseDouble(TAG_DISTANCE))); // error
- // return Double.compare(o1.get(value1), o1.get(value2)); **--> PREM answer, how to use it?**
- }
- });
- }
- }catch (JSONException e){
- e.printStackTrace();
- }
- return listMotor;
- }
- private double RoundDecimal(double distance, int i) {
- BigDecimal bd = new BigDecimal(distance);
- bd = bd.setScale(i, 6);
- return bd.doubleValue();
- }
- private double hitungJarak(double lat_asal, double lng_asal,
- double lat_tujuan, double lng_tujuan) {
- double dist;
- double radius = 6371;
- double dLat = Math.toRadians(lat_tujuan- lat_asal);
- double dLon = Math.toRadians(lng_tujuan - lng_asal);
- double a = Math.sin(dLat/2) * Math.sin(dLat/2)
- + Math.cos(Math.toRadians(lat_asal)) * Math.cos(Math.toRadians(lat_tujuan))
- * Math.sin(dLon/2) * Math.sin(dLon/2);
- double c = 2 * Math.asin(Math.sqrt(a));
- double valueResult = radius * c;
- double km = valueResult/1;
- DecimalFormat newFormat = new DecimalFormat("####");
- int kmInDec = Integer.valueOf(newFormat.format(km));
- dist = radius * c;
- return dist;
- }
- @Override
- protected void onPostExecute(ArrayList<HashMap<String, String>> listMotor) {
- // TODO Auto-generated method stub
- super.onPostExecute(listMotor);
- ListAdapter adapter = new SimpleAdapter(getActivity(), listMotor,
- R.layout.list_item, new String[] { TAG_NAMA, TAG_ALAMAT, TAG_JARAK },
- new int[] { R.id.namatb, R.id.alamattb, R.id.jarak });
- setListAdapter(adapter);
- }
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- return inflater.inflate(R.layout.list_fragment, container, false);
- }
- @Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- String pilih = getListAdapter().getItem(position).toString();
- Toast.makeText(getActivity(),
- pilih,
- Toast.LENGTH_LONG).show();
- Intent i = new Intent(getActivity(), DetailDaftar.class);
- Bundle bundle = new Bundle();
- bundle.putString("Selected", pilih);
- i.putExtras(bundle);
- startActivity(i);
- }
- }
- //coding di atas ada kesalahan dan perbaiki dgn coding berikut:
- @Override
- public int compare(HashMap<String, String> o1,
- HashMap<String, String> o2) {
- return Double.compare(Double.parseDouble(o1.get(TAG_DISTANCE)), Double.parseDouble(o2.get(TAG_DISTANCE)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement