Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass 2D array to another Activity

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");
like image 633
Christina Ayers Avatar asked Aug 31 '12 11:08

Christina Ayers


3 Answers

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);
like image 106
Chirag Avatar answered Nov 02 '22 04:11

Chirag


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];
    }
}
like image 31
Symeroro Avatar answered Nov 02 '22 05:11

Symeroro


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.

like image 30
waqaslam Avatar answered Nov 02 '22 06:11

waqaslam