【发布时间】:2015-09-29 22:55:00
【问题描述】:
我一直在想办法解决如何将多个值从一个类传递到另一个类。
我最近找到了一种方法是使用putSerializable 来做到这一点,但没有成功。我只能将最后的经度和纬度返回给其他班级。
这是我的数组 json 字符串:
{"longitude":"101.9366229","latitude":"1.236459"},
{"longitude":"101.930041","latitude":"1.224119"}]
下面是我传递值的代码:
class Findfriends extends AsyncTask<String, String, JSONObject> {
final String TAG = "Findfriends.java";
protected JSONObject doInBackground(String... args) {
// TODO Auto-generated method stub
// here Check for success tag
try {
HashMap<String, String> params = new HashMap<>();
params.put("username", args[0]);
JSONObject json = jsonParser.makeHttpRequest(
GET_FRIENDS, "POST", params);
if (json != null) {
Log.d("JSON result", json.toString());
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(JSONObject json) {
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");
Log.e(TAG, "Longitude: " + Longitude
+ ", Latitude: " + Latitude);
coordinates.setLongt(Longitude);
coordinates.setLat(Latitude);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public class Coordinates implements Serializable {
private Double lat;
private Double longt;
public Double getLat () {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLongt() {
return longt;
}
public void setLongt(Double longt) {
this.longt = longt;
}
}
取回值:
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
Coordinates coordinates=(Coordinates)bundle.getSerializable("coordinates");
System.out.println("Lat:" + coordinates.getLat());
System.out.println("Long:" + coordinates.getLongt());
【问题讨论】:
-
是的,根据您的代码,您只传递最后一项,因为坐标坐标是单个对象,而不是任何对象数组,您得到我了吗
-
@Pavan 你能写出示例代码吗?会更清楚
-
查看我的更新答案您可以提出的任何问题