所以,我有一个从 JSON 获取其值的列表:
JSONArray array = new JSONArray(jsonStr);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
JSONArray paises = jsonObject.optJSONArray("paises");
if (paises != null) {
for (int j = 0; j < paises.length(); j++) {
JSONObject jsonObject1 = paises.getJSONObject(j);
System.out.println(jsonObject1.optString("Designacao"));
String K_PAIS = jsonObject1.getString("K_PAIS");
String Designacao = jsonObject1.getString("Designacao");
String URL_IMAGE_SMALL = jsonObject1.getString("URL_IMAGE_SMALL");
String Coord_LAT = jsonObject1.getString("Coord_LAT");
String Coord_LONG = jsonObject1.getString("Coord_LONG");
String Coord_Zoom = jsonObject1.getString("Coord_Zoom");
HashMap<String, String> pais = new HashMap<>();
pais.put("K_PAIS", K_PAIS);
pais.put("Designacao", Designacao);
pais.put("URL_IMAGE_SMALL", URL_IMAGE_SMALL);
pais.put("URL_IMAGEM", URL_IMAGEM);
pais.put("Coord_LAT",Coord_LAT);
pais.put("Coord_LONG",Coord_LONG);
pais.put("Coord_Zoom",Coord_Zoom);
listaPaises.add(pais);
}
}
}
现在我想将这些值发送到我的下一个 Activity 。 这是我的点击监听器:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(view.getContext(), MapsActivity.class);
intent.putExtra( , );
startActivity(intent);
}
});
我知道它类似于 intent.putExtra( , );
,但我不知道要发送什么变量,因为各个项目的值不同。
我怎样才能做到这一点?
最佳答案
创建一个实现可序列化
的对象:
import java.io.Serializable;
public class Example implements Serializable {
private String mName;
private String mId;
private int mNumber;
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getId() {
return mId;
}
public void setId(String id) {
mId = id;
}
public int getNumber() {
return mNumber;
}
public void setNumber(int number) {
mNumber = number;
}
}
然后你可以放入这个额外的内容并以 getSerializedExtra()
intent.putExtra(EXTRA_EXAMPLE, example);
<小时/>
Example example = (Example) intent.getSerializableExtra(EXTRA_EXAMPLE);
关于java - 如何将 JSON 值发送到另一个 Activity(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42702361/