Java 如何将JSON值发送到另一个活动Android
因此,我有一个从JSON获取其值的列表: 现在我想将这些值发送到我的下一个活动。 这是我的单击侦听器:Java 如何将JSON值发送到另一个活动Android,java,android,json,android-intent,Java,Android,Json,Android Intent,因此,我有一个从JSON获取其值的列表: 现在我想将这些值发送到我的下一个活动。 这是我的单击侦听器: lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long i
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);
}
});
我知道这有点像意图;,但我不知道要发送什么变量,因为每个项目的值都不同。
如何执行此操作?创建一个实现Serializable的对象: 然后你可以把这个额外的作为getSerializableExtra 及
在适配器中创建一个方法来获取单击的项,如下所示
public Object getItemByPosition(int position){
//Array is your array of objects defined inside the adapter
return array.get(position).toString();
}
然后在项目内单击您可以执行的列表器
Intent intent = new Intent(view.getContext(), MapsActivity.class);
intent.putExtra("your_key", adapter.getItemByPosition(position));
startActivity(intent);
如果要传递数组,则创建一个对象和sendOr也没有问题。制作一个项目数组并发送。。数组必须在HashMap代码之后创建?在for中?创建您希望但必须在此事件调用之前完成的数组。是否下载数据并将其附加到列表视图?并使xyz为marc saidit返回nullfirst,使用toast检查数据是否到达,并检查从何处获取nullSo。。。使用listaPaise.addxyz;给我一个错误:ArrayList中的addjava.lang.String不能应用于com.example.abili.project.someClass,它说要更改ArrayList listaPaises=new ArrayList;to ArrayList listaPaises=新建ArrayList;你是否完全复制了我的代码?他是如何在新活动中使用适配器的?调用adapter.getItemByPosition
intent.putExtra(EXTRA_EXAMPLE, example);
Example example = (Example) intent.getSerializableExtra(EXTRA_EXAMPLE);
ArrayList<String> listpais= new ArrayList<String>(); // make this global
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");
someClass xyz= new someClass();
xyz.var1=K_data1;
xyz.var2=K_data2;
xyz.var3=K_data3;
xyz.var4=K_data4;
xyz.var5=K_data5;
listaPaises.add(xyz);
}
}
}
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( "data1", listpais );
startActivity(intent);
}
});
public Object getItemByPosition(int position){
//Array is your array of objects defined inside the adapter
return array.get(position).toString();
}
Intent intent = new Intent(view.getContext(), MapsActivity.class);
intent.putExtra("your_key", adapter.getItemByPosition(position));
startActivity(intent);