Android在单击列表项时将JSON对象的值传递给另一个活动
我想将第一个活动中单击列表项上的正确json对象传递给第二个活动。如果单击任何列表项,则来自Android在单击列表项时将JSON对象的值传递给另一个活动,android,json,Android,Json,我想将第一个活动中单击列表项上的正确json对象传递给第二个活动。如果单击任何列表项,则来自JSONArray响应的json对象应传递给intent,我的下一个活动必须获取它。 这是我的密码 JsonArrayRequest postReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override
JSONArray
响应的json对象应传递给intent,我的下一个活动必须获取它。
这是我的密码
JsonArrayRequest postReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Consumer user = new Consumer();
user.setTitle(obj.getString("name"));
user.setUserid(obj.getString("user"));
user.setThumbnailUrl(obj.getString("image"));
user.setAmountreq(obj.getString("price"));
user.setTime(obj.getString("date"));
user.setCounty(obj.getString("county"));
// adding request to consumer class
userList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
/*notifying list adapter about data changes
so that it renders the list view with updated data*/
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(postReq);
// listening to single list item on click
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(!obj.isNull(position)){
//start new activity
Intent myIntent = new Intent(RequestFeedActivity.this,RequestFeed.class);
myIntent.putExtra("name", obj.getJSONObject(position).toString());
startActivity(myIntent);
}
// ListView Clicked
}
});
}
我试过了,但没用。任何帮助都是非常感谢的。试试这个
Intent i =new Intent(this,ActivityA.class);
Bundle b =new Bundle();
i.putExtra("KEY","VALUE");
i.putExtras(b);
startActivity(i)
在接收端
Bundle b=getIntent().getExtras();
b.getInt("KEY") //useaccording to ur need
如果它不起作用,请检查此尝试替换此代码和平:
myIntent.getExtras().getString("name")
myIntent.getStringExtra("name");
使用此代码:
myIntent.getExtras().getString("name")
myIntent.getStringExtra("name");
James i尝试过,但它在if(!obj.isNull(position))和obj.getJSONObject(position).toString()上给出了一个错误,表示类型JSONObject中的方法为null不适用于参数(int)。检查编辑后的答案为什么使用JSON数据为什么没有像在onResponse中那样拆分数据。@haresh@james感谢您的回答。我查看了这个答案并尝试了这个方法,结果成功了:由于截击响应仅在onResponse(JSONArray response)方法中可见,我初始化了一个变量JSONArray数组;并指定值array=response;在setOnItemClickListener上,我添加了以下代码
if(!array.isNull(position)){Intent.putExtra(“请求”,array.optJSONObject(position.toString());startActivity(Intent);}
。在RequestFeed类Intent中,尝试{JSONObject obj=new JSONObject(myIntent.getStringExtra(“请求”);rname.setText(obj.getString(“用户”);
Yes如果(!obj.isNull(position))和这个obj.getJSONObject(position.toString(),Haresh会在这段代码中给出一个错误.James我查看了这个答案并尝试了以下方法:由于截击响应仅在onResponse(JSONArray response)方法中可见,因此我初始化了一个变量JSONArray数组;并指定了值array=response;