我正在尝试实施一个谷歌地图应用程序,将谷歌标记存储在云数据库上。但我有一个问题是要回到云上标记的坐标。
如何将AsyncTask
中生成的List返回给自定义java类?我现在遇到的问题是当我在不同的类中初始化Alistener
类时,例如,在B类中:
AListener a = new Alistener( ..., ... );
...
a.getMarkerData();
List<Pair> myList = a.getList();
myList
只是null
,我认为因为a.getList()
已经在onPostExecute()
中从该云数据库中提取数据之前执行了。任何见解都会有很大帮助。
这是我的java类:
public class AListener {
protected static final String TAG = null;
double lat, lon;
List<Pair> myList = new ArrayList<Pair>();
CustomMarkerListener( double lat, double lon ) {
this.lat = lat;
this.lon = lon;
}
public void getMarkerData() {
MarkerDataInfo ms = new MarkerDataInfo();
ms.execute( UserLogin.ITEM_URI );
}
public void setList(List<Pair> myList ) {
this.myList = myList;
}
public List<Pair> getList() {
return this.myList;
}
}
这是我的AsyncTask
AListener
的内部类:
private class MarkerDataInfo extends AsyncTask<String, Void, List<Pair>> {
List<Pair> list;
private CustomMarkerListener mLis;
public MarkerDataInfo() {}
public MarkerDataInfo( CustomMarkerListener mLis ) {
this.mLis = mLis;
}
@Override
protected List<Pair> doInBackground(String... url) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet( UserLogin.ITEM_URI);
list = new ArrayList<Pair>();
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
Log.d(TAG, data);
JSONObject myjson;
try {
myjson = new JSONObject(data);
JSONArray array = myjson.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String markerOfUser = obj.get("marker").toString();
if( markerOfUser.equals( UserLogin.accountName )) {
String latname = obj.get("lat").toString();
String lonname = obj.get("lon").toString();
double latData = Double.parseDouble(latname);
double lonData = Double.parseDouble(lonname);
list.add( new Pair( latData, lonData ));
}
}
} catch (JSONException e) {
Log.d(TAG, "Error in parsing JSON");
}
} catch (ClientProtocolException e) {
Log.d(TAG, "ClientProtocolException while trying to connect to GAE");
} catch (IOException e) {
Log.d(TAG, "IOException while trying to connect to GAE");
}
return list;
}
protected void onPostExecute(List<Pair> list) {
super.onPostExecute(list);
mLis.setList( list );
Log.d("CUstome", "" + list.size());
}
}