-1

This is my code as it is but how would I go about taking the data to another activity. I would like it to display all the information. any help will be greatly appreciated.

    try {
        JSONArray ja = new JSONArray(result);
        JSONObject jo;

        data = new String[ja.length()];
        ArrayList<String> LectureArray = new ArrayList<String>();

        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);
            data[i] = jo.getString("LectureName");

            Log.i("log_tag", "ID: " + jo.getString("ID") +
                    ", CourseCode: " + jo.getString("CourseCode") +
                    ", LectureName: " + jo.getString("LectureName") +
                    ", LectureQuestion: " + jo.getString("LectureQuestion")
            );
        }
  • Use parcelable to pass the complete object instead putting each string and then passing it. Check this [link](http://stackoverflow.com/questions/10107442/android-how-to-pass-parcelable-object-to-intent-and-use-getparcelable-method-of) – Abid Khan Apr 04 '17 at 14:58

4 Answers4

1

To pass the JSON to another activity, you have to use a Intent with a bundle:

 Intent i = new Intent(this, ToClass.class);
    i.putExtra("your_key", your_Object);
    startActivity(i); 

Then extract it from your new activity like this:

  Intent intent = getIntent();
  String easyPuzzle = intent.getExtras().getString("your_key");
Luiz Fernando Salvaterra
  • 4,102
  • 2
  • 23
  • 40
  • You should be calling `get` instead of `getString` on extras and cast the result while fetching the result. – Jimmy Apr 04 '17 at 14:56
0

You have different ways. You can use extras in the Intent, like this:

startActivity(new Intent(Activity1.this,Activity2.class).putExtra("CourseCode",1234).putExtra("LectureName","lecture1");

You can also create a parcelable object and send it as an extra too

I prefer to use Realm for persistent data which you can access everywhere

Diogo Rosa
  • 736
  • 1
  • 5
  • 24
0

You can use an Intent.

Send:

Intent i = new Intent(this, ActivityReceive.class);
i.putExtra("DATA", data);

Receive (In ActivityReceive):

String data;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        data = null;
    } else {
        data = extras.getString("DATA");
    }
} else {
    data = (String) savedInstanceState.getSerializable("DATA");
}
Marco Ferraioli
  • 152
  • 1
  • 4
  • 17
0

//To pass object of List:

intent.putExtra("MyClass", obj);

// To retrieve object in second Activity

getIntent().getSerializableExtra("MyClass");