-3

how to send the array in intent in android studio?

i have two array list

Eve_id[];//here i had stored 5 diffrent values.
sc_id[];//here i had stored 5 diffrent values.

now i have to send these two array to next activity,i have used the code like this

1st activity`

Intent k = new Intent(context, Receiver.class);
              k.putExtra("Event_id", Eve_id);
              k.putExtra("schedule_id", sc_id);`

2nd activity

 Bundle extras = getIntent().getExtras();
        long[] event_id = extras.getLongArray("Event_id");
        int[] schedule_id = extras.getIntArray("schedule_ids");

i dont know weather it is correct are not ,but i am not able to receive any data! any one can suggest how to solve this!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
selva s
  • 27
  • 1
  • 9
  • Check with it -https://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another – Adil Feb 03 '18 at 11:02

2 Answers2

0

You can use Bundle for That

 Intent passArray = new Intent(MainActivity.this ,YourSecondClass.class);
    Bundle bundle = new Bundle();
    bundle.putStringArray("my_array", array);
    startActivity(passArray , bundle);
    passArray.putExtras(bundle);
    startActivity(passArray);
meditat
  • 1,197
  • 14
  • 33
0

Use ArrayList instead of array:

ArrayList<Long> eventIds = new ArrayList<>();
ArrayList<Integer> scheduleIds = new ArrayList<>();

fill them and put into bundle:

Bundle extras = new Bundle();
extras.putIntegerArrayList("schedule_ids",scheduleIds);
extrass.putLongArrayList("Event_id", eventIds);

Intent k = new Intent(context, Receiver.class);
k.putExtras(extras);

and then extract this values in Receiver activity:

Bundle extras  = getIntent().getExtras();
ArrayList<Long> eventIds = extras.getLongArrayList("Event_id");
ArrayList<Integer> scheduleIds = extras.getIntegerArrayList("schedule_ids");
godot
  • 3,422
  • 6
  • 25
  • 42