1

I'm still NEW, i repeat, NEW in android studio and I'm trying to display place name,vicinity,lat and long using listview. I already got listview ready but I couldnt get the string from the OnPostExecute. I tried using this https://stackoverflow.com/a/12575319/5776859 but It did not work at all, or maybe I didnt do it correctly. I hope anyone could help me or show me the proper way to get the string and double from the OnPostExecute.

public class PlacesDisplayTask extends AsyncTask<Object, Integer,
 List<HashMap<String,String>>> {


    JSONObject googlePlacesJson;
    GoogleMap googleMap;

    @Override
    public List<HashMap<String, String>> doInBackground(Object... inputObj) {

        List<HashMap<String, String>> googlePlacesList = null;
        Places placeJsonParser = new Places();

        try {
            googleMap = (GoogleMap) inputObj[0];
            googlePlacesJson = new JSONObject((String) inputObj[1]);
            googlePlacesList = placeJsonParser.parse(googlePlacesJson);
        } catch (Exception e) {
            Log.d("Exception", e.toString());
        }

        return googlePlacesList;
    }

    @Override
    protected void onPostExecute(List<HashMap<String,String>> list) {

        for (int i = 0; i < 3; i++) {

            HashMap<String, String> googlePlace = list.get(i);
            double lat = Double.parseDouble(googlePlace.get("lat"));
            double lng = Double.parseDouble(googlePlace.get("lng"));
            String placeName = googlePlace.get("place_name");
            String vicinity = googlePlace.get("vicinity");

        }
    }
}
Community
  • 1
  • 1

2 Answers2

1

Use interfaces like this

public class PlacesDisplayTask extends AsyncTask<Object, Integer,
    List<HashMap<String,String>>> {

    public AsyncResponse delegate = null;
    JSONObject googlePlacesJson;
    GoogleMap googleMap;

    @Override
    public List<HashMap<String, String>> doInBackground(Object... inputObj) {

        List<HashMap<String, String>> googlePlacesList = null;
        Places placeJsonParser = new Places();

        try {
            googleMap = (GoogleMap) inputObj[0];
            googlePlacesJson = new JSONObject((String) inputObj[1]);
            googlePlacesList = placeJsonParser.parse(googlePlacesJson);
        } catch (Exception e) {
            Log.d("Exception", e.toString());
        }

        return googlePlacesList;
    }

    @Override
    protected void onPostExecute(List<HashMap<String,String>> list) {
        delegate.processFinish(list);
    }
}

and your activity should handle the response by implementing the interface's method

public class MainActivity implements AsyncResponse{
  PlacesDisplayTask asyncTask =new PlacesDisplayTask ();

  @Override
  public void onCreate(Bundle savedInstanceState) {

     //this to set delegate/listener back to this class
     asyncTask.delegate = this;

     //execute the async task 
     asyncTask.execute();
  }

  //this override the implemented method from asyncTask
  void processFinish(List<HashMap<String,String>> list){
     //Here you will receive the result fired from async class 
     //of onPostExecute(result) method.
     for (int i = 0; i < 3; i++) {

         HashMap<String, String> googlePlace = list.get(i);
         double lat = Double.parseDouble(googlePlace.get("lat"));
         double lng = Double.parseDouble(googlePlace.get("lng"));
         String placeName = googlePlace.get("place_name");
         String vicinity = googlePlace.get("vicinity");

      }
    }
 }

Create an interface like this

public interface AsyncResponse {
    void processFinish(List<HashMap<String,String>> list);
}
Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22
  • it return java.lang.NullPointerException at modularsoft.anaccident.NearbyFragment.processFinish(NearbyFragment.java:121) at modularsoft.anaccident.NearbyFragment$PlacesDisplayTask.onPostExecute(NearbyFragment.java:321) at modularsoft.anaccident.NearbyFragment$PlacesDisplayTask.onPostExecute(NearbyFragment.java:296) – Khairul Izham Jan 27 '16 at 09:39
0

A less tedious way to accomplish what you're trying to do is to perform whatever task that requires the returned value within onPostExecute(). You can input most references like context using a constructor and store them as global variables to use.

Another way can be to set the return type in the signature of the class to List<HashMap<String, String>> and you can call <PlacesDisplayTask object>.execute().get() method. But this will make the background process synchronous i.e. it will block the UI thread, waiting on the result.

The last alternative that I can think of involves the use of a delegate as described in the link you've already posted.

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
Karan Modi
  • 972
  • 2
  • 13
  • 26