1

I want to pass a 2-D String Array to another activity. My S2-D String Array (String[][]) is 'selected_list'.

code is :

     Bundle list_bundle=new Bundle();
     list_bundle.putStringArray("lists",selected_list); 
     Intent list_Intent = new Intent(v.getContext(), view_selected_items.class);
     startActivityForResult(list_Intent, 2);

But putStringArray("lists",selected_list) is showing a error like we can pass only 1-Dimensional Array (String[]).

Thanks

3 Answers 3

3

You can use putSerializable. Arrays are serializable, provided their elements are.

To store:

list_bundle.putSerializable("lists", selected_list);

To access:

String[][] selected_list = (String[][]) bundle.getSerializable("lists");
Sign up to request clarification or add additional context in comments.

2 Comments

Please tell me if we use putSerializable(), then how to retrieve the String values using getSerializable()?
Hi Matthew, When I am using that code, my process is suddently stopped. I am new in Android. Please help me. I don't know how to solve this problem. In my second activity, I need to take the String array values and display it to a TextView named 'items'. My code is: Bundle item_bundle=this.getIntent().getExtras(); String [][] selected_list = (String[][]) item_bundle.getSerializable("lists"); TextView items=(TextView) findViewById(R.id.lbl_items); selected_item=selected_list[0][0]+selected_list[0][1]; items.setText(selected_item);
1

This finally works well for me : Thanks to Matthew and Mehdiway

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];
    }
}

Comments

0

As Matthew said, you can put the string array in the bundle like this

Bundle list_bundle=new Bundle();
list_bundle.putStringArray("lists",selected_list);

This works very well, but when you try to get the array via (String[][]) bundle.getSerializable("lists"); it gives a

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.String

So what you have to do is this :

    Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("lists");
    int horizontalLength = 0;
    if (objectArray.length > 0)
        horizontalLength = ((Object[]) objectArray[0]).length; // See explanation to understand the cast
    String[][] my_list = new String[objectArray.length][horizontalLength];
    int i = 0;
    for(Object row : objectArray)
    {
        my_list[i][0] = ((String[])row)[0];
        my_list[i++][1] = ((String[])row)[1];
    }

Explanation :

When you get the array as I Serializable object it doesn't behave the same way a simple String array does, instead it considers every row as a separate array, so what you have to do is to read the Serializable object as a one dimensional array, and then cast every row of it to a String[] array. Et voila !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.