当我想从 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<>();
来自另一个类(class),这是我的 Activity ,就像这样..
ListView nearby_place = (ListView) findViewById(R.id.itemListView);
ListAdapter customAdapter = new ListAdapter(this, R.layout.nearby_place_listview, data_place);
nearby_place.setAdapter(customAdapter);
或者我的代码有问题吗..?
最佳答案
您可以通过接口(interface)获取结果。
- 创建具有所需回调方法的接口(interface)。
- 在您的 Activity 中实现它,
- 您将通过回调方法获得结果
这里我根据您的代码给您一个示例。 你的 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);
}
希望这对您有帮助。 :)
关于java - 如何从其他类(即 Activity 类)内的(AsyncTask)获取列表值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853452/