I have a scenario here,i want to pass the desired json object on the list item click in my below activity
suppose when i click deal 2
2nd json object from jArray should be passed to intent and my next activity must get it.
but problem here is that when i click any of the list items the next activity receives json object at last index .
here is my code from alldeals.java
try{
jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
Log.i("log_tag","dealid: "+json_data.getString("deal_id")+
", hotel name: "+json_data.getString("hotel_name")+
", location: "+json_data.getString("location")+
", website: "+json_data.getString("website")
);
}
json_data=new JSONObject();
String[] data=new String[jArray.length()];
planetList = new ArrayList<String>();
for(int i=0;i<jArray.length();i++)
{
json_data= jArray.getJSONObject(i);
data[i]=json_data.getString("deal_id");
Log.i("log_tag", "string "+data[i]);
planetList.addAll( Arrays.asList("Deal "+ (i+1)));
listAdapter = new ArrayAdapter<String>(this, R.layout.listrow, planetList);
runOnUiThread(new Runnable() {
public void run() {
list.setAdapter(listAdapter);
}
});
}
list.setOnItemClickListener(new OnItemClickListener() { //where to put this piece of code???
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
Intent intent = new Intent(context,Finaldeal.class);
intent.putExtra("deal", json_data.toString());
startActivity(intent);
}
});
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
i know in order to solve this i have to put my list.setOnItemClickListener()
at proper place but i am confused where to put it,so that when i click on deal1
the 1st json object will be passed to intent
If you want it to be more flexible, you can put the JSON data you will pass in an
ArrayList
andget()
the data you want to pass automatically depending on the item clicked like so:Then for your listener, you can simply put it right after you define your list.
you can pass
JsonObject
from JsonArray according to ListView item clicked Position as:currently you are passing
json_data
which always contain last JSONObject from JSONArray when for loop execution endI know its too late to post this answere but your question is ranking on no.1 position in google search engine, so if someone hasn't find the solution then might be my answere help them. For passing json data to another activity according to Listview position:
Do this inside OnItemClick you need to copy this code:
JSONObject SelectedItem=(yourjsonresponse).getJsonObject(position);
and same as for JSONArray.
then pass SelectedItem.toString in your Intent.
Note : yourjsonresponse is your Main API Response that can be jsonobject or jsonarray.
Thank you