1

I have a problem when I want to get the value from a global variable inside AsyncTask class. I want to call it from another class..

Here's my AsyncTask class code:

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

    List<NearbyPlaceModel> data_place = new ArrayList<>();
    JSONObject googlePlacesJson;
    GoogleMap googleMap;

    @Override
    protected 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) {
        googleMap.clear();
        data_place.clear();
        for (int i = 0; i < list.size(); i++) {
            if(i < 10 ) {
                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");
                LatLng latLng = new LatLng(lat, lng);
                markerOptions.icon(BitmapDescriptorFactory.fromBitmap(Configure.getBitmapFromURL(googlePlace.get("icon"))));
                markerOptions.title(placeName + " : " + vicinity);
                googleMap.addMarker(markerOptions);

                NearbyPlaceModel items = new NearbyPlaceModel();
                items.setVicinity(vicinity);
                items.setPlace_name(placeName);
                items.setLat(String.valueOf(lat));
                items.setLang(String.valueOf(lng));
                data_place.add(items);
            }
        }
    }

}

I want to set the global variable data_place = new ArrayList<>(); from another class which is my activity, like this..

ListView nearby_place = (ListView) findViewById(R.id.itemListView);
    ListAdapter customAdapter = new ListAdapter(this, R.layout.nearby_place_listview, data_place);
    nearby_place.setAdapter(customAdapter);

Or did I something wrong with my code..?

Bom Tomdabil
  • 61
  • 1
  • 10
Iam ByeBlogs
  • 715
  • 1
  • 6
  • 15
  • 1
    Possible duplicate of [How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) – OneCricketeer Oct 04 '16 at 13:43

2 Answers2

0

If I understand it correctly! In your AsyncTask class create getter and setter method. This way you can get data_place and set data_place from your activity.

    public List<NearbyPlaceModel> getData_place() {
            return data_place;
        }

    public void setData_place(List<NearbyPlaceModel> data_place) {
            this.data_place = data_place;
        }
Muhammed GÜNEŞ
  • 304
  • 2
  • 15
0

You can get the result by using an interface.

  1. Create an interface with required callback method.
  2. Implement it on your activity,
  3. You will get the result on your callback method

Here I am giving you an example based on your code. Your MainActivity class should be like this-

 public class MainActivity extends AppCompatActivity implements PlaceInterface{

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         // your code...

         // calling the AsyncTask and send reference of the interface on it
         new PlacesDisplayTask(this).execute();  // something like this

     }


     @Override
     public void myPlaceList(ArrayList<NearbyPlaceModel>   nearbyPlaceModelArrayList) {
         ListView nearby_place = (ListView)  findViewById(R.id.itemListView);
         ListAdapter customAdapter = new ListAdapter(this, R.layout.nearby_place_listview, nearbyPlaceModelArrayList);
         nearby_place.setAdapter(customAdapter);
     }
}

Your PlacesDisplayTask class should be like this-

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

     ArrayList<NearbyPlaceModel> data_place = new ArrayList<>();   // change
     JSONObject googlePlacesJson;
     GoogleMap googleMap;
     PlaceInterface placeInterface;    // change

     public PlacesDisplayTask(PlaceInterface placeInterface){   // change
         this.placeInterface = placeInterface;
     }

     @Override
     protected 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) {
         googleMap.clear();
         data_place.clear();
         for (int i = 0; i < list.size(); i++) {
             if(i < 10 ) {
                 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");
                 LatLng latLng = new LatLng(lat, lng);
             markerOptions.icon(BitmapDescriptorFactory.fromBitmap(Configure.getBitmapFromURL(googlePlace.get("icon"))));
                 markerOptions.title(placeName + " : " + vicinity);
                 googleMap.addMarker(markerOptions);

                 NearbyPlaceModel items = new NearbyPlaceModel();
                 items.setVicinity(vicinity);
                 items.setPlace_name(placeName);
                 items.setLat(String.valueOf(lat));
                 items.setLang(String.valueOf(lng));
                 data_place.add(items);

                 placeInterface.myPlaceList(data_place);  // change
             }
         }
     }

 }

And this is the Interface-

 public interface PlaceInterface {
     void myPlaceList(ArrayList<NearbyPlaceModel> nearbyPlaceModelArrayList);
 }

Hope this will help you. :)

D_Alpha
  • 4,039
  • 22
  • 36