0

i want to pass the correct json object on the list item click in the first activity to the second activity. If i click any list item, the json object from JSONArray response should be passed to intent and my next activity must get it. Here is my code

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





              }
            });
    }

On the other activity here is my sample code of the variables i want to be passed

TextView rname, rtitle,rprice, rdate, rdesc, rcounty;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.requestfeed);

    rtitle = (TextView)findViewById(R.id.IDRproduct);
    rprice = (TextView)findViewById(R.id.IDRquote);
    rdate = (TextView)findViewById(R.id.IDRdate);
    rdesc = (TextView)findViewById(R.id.IDRdesc);
    rname = (TextView)findViewById(R.id.IDRname);

     Intent myIntent = getIntent();
    //Assign values
    rname.setText(myIntent.getExtras().getString("name")); 
    rtitle.setText(myIntent.getExtras().getString("title"));
    rprice.setText(myIntent.getExtras().getString("pricetag"));
    rdate.setText(myIntent.getExtras().getString("timereq"));
    rdesc.setText(myIntent.getExtras().getString("description"));
    rcounty.setText(myIntent.getExtras().getString("county")); 




}

I tried this answer but didnt work .Any much help is much appreciated

4
  • James i tried but it gives an error on if(!obj.isNull(position)) and obj.getJSONObject(position).toString() saying the the method is null in the type JSONObject is not applicable for arguments (int). Commented Aug 23, 2014 at 12:16
  • Check th edited answer Commented Aug 23, 2014 at 12:21
  • Why you are using the JSON data why you are not spliting the data as you have done in onResponse. Commented Aug 23, 2014 at 13:01
  • @haresh @james thanks for your answers.I looked at that answer and tried this and it worked:Since the volley response is only visible in the onResponse(JSONArray response) method i initialized a variable JSONArray array; and assigned the value array= response;On the setOnItemClickListener i added the following code if(!array.isNull(position)){ Intent.putExtra("request",array.optJSONObject(position).toString()); startActivity(intent); } . In RequestFeed class Intent try { JSONObject obj = new JSONObject(myIntent.getStringExtra("request")); rname.setText(obj.getString("user")); Commented Aug 23, 2014 at 21:02

2 Answers 2

0

Try to replace this peace of code:

myIntent.getExtras().getString("name")

With this code:

myIntent.getStringExtra("name");
Sign up to request clarification or add additional context in comments.

1 Comment

Yes Haresh gives an error on this code if(!obj.isNull(position)) and this obj.getJSONObject(position).toString() .
0

Just try this

Intent i =new Intent(this,ActivityA.class);
Bundle b =new Bundle();
i.putExtra("KEY","VALUE");
i.putExtras(b);
startActivity(i)

At the receiver side

Bundle b=getIntent().getExtras();
b.getInt("KEY") //useaccording to ur need

If it doesn't work check this answer

1 Comment

James i looked at that answer an tried this: Since the volley response is only visible in the onResponse(JSONArray response) method i initialized a variable JSONArray array; and assigned the value array = response;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.