Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. private class DownloadJSONItems extends AsyncTask<Void, Void, Void> {
  2.  
  3. @Override
  4. protected void onPreExecute() {
  5. super.onPreExecute();
  6. // Create a progressbar
  7. if (!prefs) {
  8. progressBar.setVisibility(View.VISIBLE);
  9. }
  10. }
  11.  
  12. @Override
  13. protected Void doInBackground(Void... params) {
  14. // Create an array
  15. arraylist = new ArrayList<HashMap<String, String>>();
  16. // Retrieve JSON Objects from the given URL address
  17. jsonobject = JSONfunctions
  18. .getJSONfromURL(
  19. "Server URL",
  20. latitude, longitude);
  21.  
  22. try {
  23. // Locate the array name in JSON
  24. jsonarray = jsonobject.getJSONArray("places");
  25.  
  26. for (int i = 0; i < jsonarray.length(); i++) {
  27. HashMap<String, String> map = new HashMap<String, String>();
  28. jsonobject = jsonarray.getJSONObject(i);
  29.  
  30. double convertString = Double.parseDouble(jsonobject
  31. .getString("distance"));
  32. double convertKMToMile = convertString * 0.6124;
  33. String convertedValue = String.format("%.2f",
  34. convertKMToMile);
  35. // Retrive JSON Objects
  36. map.put("places_name", jsonobject.getString("places_name"));
  37. map.put("distance", convertedValue);
  38. map.put("places_icon", jsonobject.getString("places_icon"));
  39. // Set the JSON Objects into the array
  40. arraylist.add(map);
  41. }
  42. } catch (JSONException e) {
  43. Log.e("Error", e.getMessage());
  44. e.printStackTrace();
  45. }
  46. return null;
  47. }
  48.  
  49. @Override
  50. protected void onPostExecute(Void args) {
  51.  
  52. //Toast.makeText(getActivity(), getString(jsonarray.length()), 6000).show();
  53. // Pass the results into ListViewAdapter.java
  54. adapter = new ListViewAdapter(getActivity(), arraylist);
  55. // Set the adapter to the ListView
  56. listview.setAdapter(adapter);
  57. // Close the progressbar
  58. progressBar.setVisibility(View.GONE);
  59. if (prefs) {
  60. prefs = false;
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement