我想從AsyncTask類中的全局變量中獲取值時遇到問題。我想從另一個類調用它..如何從(AsyncTask)中獲取列表值,其他類是活動類..?
這裏是我的AsyncTask類代碼:
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);
}
}
}
}
我想從另一個類,這是我的活動,這樣的設置全局變量data_place = new ArrayList<>();
..
ListView nearby_place = (ListView) findViewById(R.id.itemListView);
ListAdapter customAdapter = new ListAdapter(this, R.layout.nearby_place_listview, data_place);
nearby_place.setAdapter(customAdapter);
還是我的代碼有問題..?
[如何獲取OnPostExecute()的結果到主要活動的可能的重複,因爲AsyncTask是一個單獨的類?](http://stackoverflow.com/questions/12575068/how-to-get-the-結果的onpostexecute到主要活動因爲asynctask是a) –