how to send the array by using an intent?

时间:2018-02-03 10:56:31

标签: android android-intent

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!

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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");