-4

This code below returns json into a textView. How do I pass the data within this textview to another activity?

 private void showJSON(String response){
    String name="";
    String address="";
    String vc = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString(Config.KEY_NAME);
        address = collegeData.getString(Config.KEY_ADDRESS);
        vc = collegeData.getString(Config.KEY_VC);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}

This is the tutorial link I'm following. I know it can be done using intents but I wasn't sure how to use it in this example.

Thanks

Mkuuu7
  • 13
  • 1
  • 5

3 Answers3

0
String json=textViewResult.getText().toString();
startActivity(new Intent(this,YourActivity.class).putExtra("data",json));

Is this what you want?

Lucky Dog
  • 608
  • 5
  • 15
0

Pass String data using putExtra

    Intent i = new Intent(youractivity.this, SecondActivity.class);
    i.putExtra("name",name);
    i.putExtra("address",address);
    i.putExtra("vc",vc);
    startActivity(i);

In SecondActivity

    String name=getIntent().getStringExtra("name"); 
    String address=getIntent().getStringExtra("address"); 
    String vc=getIntent().getStringExtra("vc"); 

OR

Pass json data from activity

     Intent i = new Intent(youractivity.this, SecondActivity.class);
     intent.putExtra("jsonObject", jsonObject.toString());
     startActivity(i);

In SecondActivity

      Intent intent = getIntent();
      String jsonString = intent.getStringExtra("jsonObject");
      JSONObject jsonObject = new JSONObject(jsonString);
Komal12
  • 3,340
  • 4
  • 16
  • 25
0

Thank you all for the answers. This has helped me understand how to use intents and I was able to solve my problem with this:

Activity 1

    String json= textViewResult.getText().toString();
    Intent i = new Intent(Activity1.this, Activity2.class);
    i.putExtra("Data",json);
    startActivity(i);

Activity 2

    Bundle b = getIntent().getExtras();
    String json = b.getString("Data");
    TextView jData = (TextView) findViewById(R.id.textView1);
    jData.setText(json);
Mkuuu7
  • 13
  • 1
  • 5