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.

buræquete
  • 14,226
  • 4
  • 44
  • 89
Modest
  • 53
  • 1
  • 7
  • Possible duplicate of [Pass 2D array to another Activity](https://stackoverflow.com/questions/12214847/pass-2d-array-to-another-activity) – unzila Jul 24 '19 at 04:30

3 Answers3

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!

Xavi Jimenez
  • 314
  • 1
  • 3
  • 16
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");
NJY404
  • 349
  • 3
  • 14
  • I have tried this, and it is giving me the following error. . . Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList>' – Modest Jul 24 '19 at 04:05
  • Did the error occur on run time or on the compilation? – NJY404 Jul 24 '19 at 04:30
  • If it's just a warning, try to run your code. It should work. – NJY404 Jul 24 '19 at 04:39
  • 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. – Modest Jul 24 '19 at 05:38
  • Just make sure you're parsing the same data type/object. Since we are parsing the same ```ArrayList>``` it would definitely work. – NJY404 Jul 24 '19 at 06:32
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");
Rajat Beck
  • 1,523
  • 1
  • 14
  • 29
  • I have tried this, it too gives me the same error as NJY404's response: Unchecked cast: 'java.io.Serializable' to 'java.util.ArrayList>' – Modest Jul 24 '19 at 05:05
  • Read [this](https://stackoverflow.com/questions/51805648/unchecked-cast-java-io-serializable-to-java-util-arraylist) link and you will get the clarification as to why it is showing the warning and how to prevent it. – Rajat Beck Jul 24 '19 at 06:23