简体   繁体   中英

Send Json data from listview in one activity to another activity

I am trying to send the json object data from one activity to other activity. I have a listview that is successfully populated from a json array, but as i click on a list item, it is not responding. No action is being performed on item click. I saw some tutorials but it did not helped. I need to send the list item's json data to another activity. Any help would be appreciated.

Activity of listview populated with Json.

       try{

         jArray = new JSONArray(result);
         ListView listView=(ListView)findViewById(R.id.listtour_cat);
         listView.setAdapter(new ListViewAdapter(JSONUseActivity.this,jArray));
         listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                try{
                    if(!jArray.isNull(arg2)){
                           Intent intent = new Intent(getApplicationContext(),TourDetailActivity.class);  
                           intent.putExtra("id", jArray.optJSONObject(arg2).getString("tour_id"));
                           startActivity(intent);    
                     }

                }
                catch(JSONException e){
                       Log.e("log_tag", "Error parsing data "+e.toString());
               }

            }
        });
       }
       catch(JSONException e){
               Log.e("log_tag", "Error parsing data "+e.toString());
       }

  }
        catch (Exception e) {
          Log.e("log_tag","Error in http connection!!" + e.toString());     
  }

Target Activity:

Intent intent = getIntent();
String rcvid = intent.getStringExtra("id");
Toast.makeText(getApplicationContext(), rcvid, Toast.LENGTH_LONG).show();

Convert JsonArray to String then attach it to Intent ans send it.

    JSONObject jObject = new JSONObject("Your Json Response");

    Intent obj_intent = new Intent(Main.this, Main1.class);
    Bundle b = new Bundle();                
    b.putString("Array",jObject4.toString());
    obj_intent.putExtras(b);

Where jObject4 is JSON Object.

Get in next Page :

        Intent b = getIntent().getExtras();
        String Array=b.getString("Array");

Try this line..

Intent intent = new Intent(getApplicationContext(),TourDetailActivity.class);  
intent.putExtra("id", jArray.getJSONObject(arg2).getString("tour_id"));
startActivity(intent);    

put this code for getting value in second class..

String rcvid = getIntent().getExtras().getString("id").toString();

And pass value from First class with intent like:

intent.putExtra("id", jArray.optJSONObject(arg2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM