我仍然是新的,我重复一遍,在android工作室中新的,我正在尝试使用listview显示地名,附近,纬度和长期。我已经准备好了listview但是我无法从OnPostExecute获取字符串。我尝试使用这个https://stackoverflow.com/a/12575319/5776859,但它根本不起作用,或者我没有正确地做到这一点。我希望有人可以帮助我,或者告诉我从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");
}
}
}
答案 0 :(得分:1)
使用这样的界面
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);
}
}
并且您的活动应该通过实施界面方法
来处理响应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");
}
}
}
创建一个像这样的界面
public interface AsyncResponse {
void processFinish(List<HashMap<String,String>> list);
}
答案 1 :(得分:0)
完成您尝试做的事情的一种不那么繁琐的方法是在onPostExecute()
内执行任何需要返回值的任务。您可以使用构造函数输入大多数引用(如上下文),并将它们存储为要使用的全局变量。
另一种方法是将类签名中的返回类型设置为List&lt; HashMap<String, String>>
,然后调用<PlacesDisplayTask object>.execute().get()
方法。但这会使后台进程同步,即它会阻塞UI线程,等待结果。
我能想到的最后一个选择涉及使用您已经发布的链接中描述的代理。