8

how do i pass 2 denominational array object as a parameter to another activity

how to get two dimensional array string value in another activity

   String [][]str;

    Intent l = new Intent(context,AgAppMenu.class);
                 l.putExtra("msg",str);
                 l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
                 context.startActivity(l);



  another Activity class

   String[][] xmlRespone2;

    xmlRespone2 = getIntent().getExtras().getString("msg");
BC2
  • 892
  • 1
  • 7
  • 23
Christina Ayers
  • 97
  • 1
  • 1
  • 5

5 Answers5

14

You can use putSerializable. Arrays are serializable.

To store:

bundle.putSerializable("list", selected_list); // Here bundle is Bundle object.

To access:

String[][] passedString_list = (String[][]) bundle.getSerializable("list");

Example

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("list", selected_list);
mIntent.putExtras(mBundle);
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • when i access this object in diffent classs bunvle makes error – Christina Ayers Aug 31 '12 at 12:17
  • 1
    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.agappmenu); mno = getIntent().getExtras().getString("mno"); pinno = getIntent().getExtras().getString("pinno"); String[][] xmlRespone2 = (String[][]) bundle.getSerializable("msg"); – Christina Ayers Aug 31 '12 at 12:18
  • bundle not resolve in another activity where i access this value – Christina Ayers Aug 31 '12 at 12:18
  • Try this . Bundle bundle = getIntent().getExtras(); (String[][]) bundle.getSerializable("msg"); – Chirag Aug 31 '12 at 12:19
  • 3
    @ChiragRaval Hi, I tried doing your steps, but when I got to `resultMatrix = (double[][]) bundle.getSerializable("resultMatrix_");`, I got the exception `java.lang.Object[] cannot be cast to double[][]`. – Don Larynx May 17 '15 at 19:35
6

This finally works well for me :

To start a new activity (sending String[][] and String):

String[][] arrayToSend=new String[3][30];
String stringToSend="Hello";
Intent i = new Intent(this, NewActivity.class);

i.putExtra("key_string",stringToSend);

Bundle mBundle = new Bundle();
mBundle.putSerializable("key_array_array",  arrayToSend);
i.putExtras(mBundle);

startActivity(i);

To access in NewActivity.onCreate:

String sReceived=getIntent().getExtras().getString("key_string");

String[][] arrayReceived=null;
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("key_array_array");
if(objectArray!=null){
    arrayReceived = new String[objectArray.length][];
    for(int i=0;i<objectArray.length;i++){
        arrayReceived[i]=(String[]) objectArray[i];
    }
}
Symeroro
  • 71
  • 1
  • 1
2

You may define a custom class which implements Parcelable and contains logic to read and write 2-dimensional-array from/to Parcel. Afterwards, put that parcelable object inside Bundle for transportation.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

set the 2darray as public static void. let current_class be the class in which we create our 2d array We want to pass the data to NewActivity

Class<?> ourClass=Class.forName("com.example.testapp.NewActivity");
Intent ourIntent= new Intent(current_class.this,ourClass);
intent_name.putExtra("name", 2darray_name);
startActivity(ourIntent);`

to access this in NewActivity , use current_class.2darray_name where current_class is the class where it was originally defined.

ben_joseph
  • 1,660
  • 1
  • 19
  • 24
-1

one Solution is that you can set it as Static so that you can use that in any of your activity.

Class A{
 public static String [][]str;
...
    Intent l = new Intent(context,AgAppMenu.class);
                 l.putExtra("msg",str);
                 l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
                 context.startActivity(l);


}

Class B{

...
you can use it with Just A.(ArrayName)
System.out.println(A.str);

}

Hope it will Help you.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107