当我想从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);
或者我的代码出了什么问题......?
答案 0 :(得分:0)
如果我理解正确的话!在AsyncTask类中创建getter和setter方法。这样,您就可以从活动中获取data_place并设置data_place。
public List<NearbyPlaceModel> getData_place() {
return data_place;
}
public void setData_place(List<NearbyPlaceModel> data_place) {
this.data_place = data_place;
}
答案 1 :(得分:0)
您可以使用界面获得结果。
我在这里根据你的代码给你一个例子。 您的MainActivity类应该是这样的 -
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);
}
}
您的PlacesDisplayTask类应该是这样的 -
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
}
}
}
}
这就是界面 -
public interface PlaceInterface {
void myPlaceList(ArrayList<NearbyPlaceModel> nearbyPlaceModelArrayList);
}
希望这会对你有所帮助。 :)