0

I have

Integer[] mainArray;

In one of my activity which was filled randomly with unique values.
Now I want this array to copied to next activity?
How can be achieved? I have seen that there seems to be no option to carry it with

Bundle b=new Bundle();
b.putIntArray("key", mainArray);
Intent i=new Intent(context, Class);
i.putExtras(b);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nandroid
  • 1
  • 1
  • 1
  • you have right code for sending Array to next Activity then what is issue ? yes u have one more operation by create a singleton class to share data between Application components – ρяσѕρєя K Feb 04 '13 at 13:43
  • with above code, eclipse says :The method putIntArray(String, int[]) in the type Bundle is not applicable for the arguments (String, Integer[]) – Nandroid Feb 05 '13 at 10:13
  • then just use Intent.putExtra for sending Integer array – ρяσѕρєя K Feb 05 '13 at 10:19

3 Answers3

0

It is exactly what you need :

Bundle b=new Bundle();
b.putIntArray("key", mainArray);
Intent i=new Intent(context, Class);
i.putExtras(b);

Just get the data from it on your other activity,
on onCreate() :

int data[] = getIntent().getIntArrayExtra("key");

What is the problem?

There is a difference between int and Integer in Java. You can find out more about it here : What is the difference between an int and an Integer in Java and C#?

Community
  • 1
  • 1
EvZ
  • 11,889
  • 4
  • 38
  • 76
  • with above code, eclipse says :The method putIntArray(String, int[]) in the type Bundle is not applicable for the arguments (String, Integer[]) – Nandroid Feb 05 '13 at 07:57
  • Please take a look : http://stackoverflow.com/questions/564/what-is-the-difference-between-an-int-and-an-integer-in-java-c – EvZ Feb 05 '13 at 09:53
0

In the activity sending the integer array use:

intent.putExtra("array", mainArray);

In the activity receiving this integer array use:

int array[] = getIntent().getIntArrayExtra("array")
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

Try this,

Instead of integer array use arraylist.

Intent i=new Intent(context, Class);
 i.putIntegerArrayListExtra("name", arraylist);
 startActivity(i);

Other this integer arraylist to next activity

 Intent i = getIntent();
 ArrayList<String> list = new ArrayList<String>();
 list = i.getStringArrayListExtra("name");
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22