1

I am trying to pass an a multidimensional arraylist (ex: Arraylist>) from one java activity to another. I've looked at many posts and have determined using a bundle is the best way to go (i am also already using a bundle elsewhere so i know how they generally work), however I haven't seen an example of a 2d arraylist.

I've tried something along the line of the following, but didn't get far after realizing putStringArrayListExtra doesnt accept 2d arraylists:

private static ArrayList<ArrayList<String>> bigArrayList = new ArrayList<>();
static ArrayList<String> smallArrayList= new ArrayList<>();

Bundle b = new Bundle();
b.putStringArrayListExtra("2dArrayList", bigArrayList );
Intent i=new Intent(context, Class);
i.putExtras(b);

ArrayList<String> urls = getIntent().getStringArrayListExtra("2dArrayList");

Just looking for some help or advice on how to pass 2d arraylists between activities.

1

3 Answers 3

1

You can use Gson library for this, no need to implement serializable.

Suppose your arraylist is :

ArrayList<ArrayList<String>> bigArrayList= new ArrayList<>();

After that, you can add it to the intent as below :

Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("data", new Gson().toJson(bigArrayList));

You can later retrieve this in Activity2 as below :

String extra = getIntent().getStringExtra("data");
ArrayList<ArrayList<String>> bigArrayList= new Gson().fromJson(extra, new TypeToken<ArrayList<ArrayList<String>>>(){}.getType());

It works for me! I hope It helps you!

Sign up to request clarification or add additional context in comments.

Comments

0

I haven't tried this yet. But I hope this helps. I have tried the code, It's working.

Current Activity

private static ArrayList<ArrayList<String>> bigArrayList = new ArrayList<>();
Intent i=new Intent(context, Class);
i.putExtra("bigArrayList", bigArrayList);

Next Activity

Parse the serialize data to ArrayList<ArrayList<String>>, to pass the value to urls variable.

ArrayList<ArrayList<String>> urls = (ArrayList<ArrayList<String>>) getIntent().getSerializableExtra("bigArrayList");

5 Comments

I have tried this, and it is giving me the following error. . . Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList<java.util.ArrayList<java.lang.String>>'
Did the error occur on run time or on the compilation?
If it's just a warning, try to run your code. It should work.
It works when the code runs, but just wondering if the warning is something i can ignore or will affect performance later on and should be resolved now.
Just make sure you're parsing the same data type/object. Since we are parsing the same ArrayList<ArrayList<String>> it would definitely work.
0

ActivityA.java

        Intent intent = new Intent(this, TestActivity.class);
        ArrayList<ArrayList<String>> ddArray = new ArrayList<ArrayList<String>>();
        ArrayList<String> a = new ArrayList<String>();
        a.add("A");
        ArrayList<String> b = new ArrayList<String>();
        b.add("B");
        ddArray.add(a);
        ddArray.add(b);
        Bundle bundle = new Bundle();
        bundle.putSerializable("ARRAY",ddArray);
        intent.putExtras(bundle);
        startActivity(intent);

TestActivity.java

 ArrayList<ArrayList<String>> array = (ArrayList<ArrayList<String>>) getIntent().getExtras().getSerializable("ARRAY");

2 Comments

I have tried this, it too gives me the same error as NJY404's response: Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList<java.util.ArrayList<java.lang.String>>'
Read this link and you will get the clarification as to why it is showing the warning and how to prevent it.

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.