我使用aysctask从Web服务获取数据。我想要从字符串中获得的任何数据,该数据添加到ArrayList markerPoints arraylist。这是在oncreate声明,所以我如何将数据添加到arraylist.pls帮助我,我是Android的新手。提前谢谢。
protected void onPostExecute(String response) {
super.onPostExecute(response);
try {
JSONObject jsonResponse = new JSONObject(response);
JSONObject jArray = jsonResponse.getJSONObject("data");
JSONObject jstart = jArray.getJSONObject("start");
JSONObject jend = jArray.getJSONObject("end");
JSONObject jcureent = jArray.getJSONObject("current");
for (int i = 0; i < jArray.length(); i++) {
startlat=jstart.getString("lat");
startlong=jstart.getString("long");
endtlat=jend.getString("lat");
endlong=jend.getString("long");
currentlat=jcureent.getString("lat");
currentlong=jcureent.getString("long");
}
在oncreate我声明了, markerPoints = new ArrayList();我创建了一个arraylist,我想要从存储到该数组的json获得的任何数据。谢谢
答案 0 :(得分:0)
将ArrayList
声明为全局变量:
public class MainActivity extends Activity {
private ArrayList myArrayList;
protected void onCreate(Bundle bundle) {
...
myArrayList = new ArrayList();
...
}
private final class MyAsyncTask extends AsyncTask {
...
protected void onPostExecute(String response) {
super.onPostExecute(response);
try {
JSONObject jsonResponse = new JSONObject(response);
JSONObject jArray = jsonResponse.getJSONObject("data");
JSONObject jstart = jArray.getJSONObject("start");
JSONObject jend = jArray.getJSONObject("end");
JSONObject jcureent = jArray.getJSONObject("current");
for (... ) {
myArrayList.add(jstart.getString("lat"));
myArrayList.add(jstart.getString("long"));
myArrayList.add(jend.getString("lat"));
myArrayList.add(jend.getString("long"));
myArrayList.add(jcureent.getString("lat"));
myArrayList.add(jcureent.getString("long"));
}
} catch(...) { }
}