【发布时间】:2015-09-26 06:21:56
【问题描述】:
我正在尝试使用 HashMap 将值传递给其他类,但我不确定为什么会得到如此奇怪的结果。我做了一个 system.out.println 并得到 [{Latitude=1.224119, username=borrow, Longitude=103.930041}],[{Latitude=1.224119, username=borrow, Longitude=103.930041}] 重复但不应该是这样因为我有两个不同的坐标。
当我将它传递给另一个类时,它只返回一个坐标而不是两个。
我的代码有什么问题吗?
从 db 中获取坐标并传递给另一个类:
protected void onPostExecute(JSONObject json) {
ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
HashMap<String, String> h1=new HashMap<String, String>();
if (json != null) {
Toast.makeText(Borrower_AP.this, json.toString(),
Toast.LENGTH_LONG).show();
try {
dataJsonArr = json.getJSONArray("Posts");
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
Longitude = c.getDouble("longitude");
Latitude = c.getDouble("latitude");
String s_lat = Latitude.toString();
String s_long = Longitude.toString();
h1.put("Latitude",s_lat);
h1.put("Longitude",s_long);
h1.put("username",username);
list.add(h1);
Intent i = new Intent(this, BorrowerMap.class);
i.putExtra("list", list);
System.out.print(list);
startActivity(i);
}
取出值:
ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
list=(ArrayList<HashMap<String, String>>) getIntent().getExtras().get("list");
String Latitude=list.get(0).get("Latitude");
String Longitude=list.get(0).get("Longitude");
String s_lat = Latitude.toString();
String s_long = Longitude.toString();
System.out.println(Latitude);
System.out.println(Longitude);
【问题讨论】: