1

Is there a way to pass an ArrayList <ArrayList<Integer>> floors to another activity through Bundle?

Thanks

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Ang3lo
  • 77
  • 1
  • 1
  • 9

2 Answers2

6

Is there a way to pass an ArrayList > floors to another activity through Bundle?

Unfortunetly not.

If you would have ArrayList without nested it will work with putIntegerArrayList(key, value) and getIntegerArrayList(key).

But there is for sure another approach(es).I will explain you one possible way.

You can create class that will implement Serializable interface and in this class just create field and appropriate getter. I will give you basic example. Then you will pass Serializable through Activities.

public class DataHelper implements Serializable {

   private ArrayList<ArrayList<Integer>> floors;

   public DataHelper(ArrayList<ArrayList<Integer>> floors) {
      this.floors = floors;
   }

   public ArrayList<ArrayList<Integer>> getList() {
      return this.floors;
   }
}

Save it to Bundle:

Bundle b = new Bundle();
b.putSerializable("floors", new DataHelper(floors));

and retrieve in target Activity:

getIntent().getExtras().getSerializable("floors");
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • It gives the following error: The method putIntegerArrayList(String, ArrayList) in the type Bundle is not applicable for the arguments (String, ArrayList>) ArrayList> and not an ArrayList – Ang3lo Mar 10 '13 at 16:48
  • @sajmon_d : but i think it possible to send nested ArrayList – ρяσѕρєя K Mar 10 '13 at 17:01
  • 2
    @ρяσѕρєяK do you think putParcelableArrayList? but it requires ArrayList exteds Parcelable> and it won't work for Integer. I know that Parcelable is maybe better way how to pass data through Activities but Serializable work too and mainly is easer to understand and require less code to write. – Simon Dorociak Mar 10 '13 at 17:02
  • @sajmon_d : yes u are right but like ur answer using Serializable – ρяσѕρєя K Mar 10 '13 at 17:09
  • 1
    @ρяσѕρєяK I just created "wrapping" class that wraps ArrayList and class implements Serializable and now you are able to putSerializable to Bundle and pass through Activities and simply in second Activity you will retrieve whole Serializable class and with getter get ArrayList and work is done. – Simon Dorociak Mar 10 '13 at 17:12
  • @ρяσѕρєяK for sure, if you will have ArrayList of own defined objects for example Foo then ArrayList() so in this case also Foo must implements Serializable otherwise it won't work and Exception will be thrown. – Simon Dorociak Mar 10 '13 at 17:13
  • @sajmon_d : but im agree with u and i think ur suggestion is in right direction .Thanks – ρяσѕρєя K Mar 10 '13 at 17:18
  • Thanks dude, I followed this suggestion and it solved my problem. Instead of passing the ArrayList now I pass the entire object to the other activity and there I use the get method to obtain the ArrayList. thanks :) – Ang3lo Mar 10 '13 at 17:52
  • @sajmon_d: since `ArrayList` already implements `Serializable`, why would you wrap it inside yet another serializable layer? Without trying, I'm reasonably sure you should be able to add an `ArrayList>` instance as serializable directly to the intent/bundle. – MH. Mar 10 '13 at 18:16
  • I didn't notice that and you are right, I'm using this approach now, i.e. I'm passing the ArrayList> directly. However when I do this: this.floorList= (ArrayList>) buildingList.getSerializable("floorList"); I get the following warning: Type safety: Unchecked cast from Serializable to ArrayList> Do you know why? – Ang3lo Mar 10 '13 at 19:43
  • @Ang3lo: You can basically ignore (or suppress) that warning. The problem is that a cast is a runtime check, but due to type erasure, at runtime there's actually no difference between e.g. `ArrayList>` and `ArrayList>`, or any other `Foo` and `Bar`. Since in this case you fully control the input and output of the `Intent` extras, you should never run into a `ClassCastException` at runtime. – MH. Mar 11 '13 at 07:47
1

To pass the arraylist from first activity to second activity.

Intent intent = new Intent(context, SecondActity.class);
intent.putIntegerArrayListExtra("arraylist",integerList);  //integerList is ArrayList<Integer>
startActivity(intent);

To get the arrayList in second Activity.

ArrayList arrayList<Integer> = getIntent().getIntegerArrayListExtra("arraylist")

Read here.

If you want to pass the custom object between activities then read this thread.

Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • 2
    it wont work. he has ArrayList with nested ArrayList :- – Simon Dorociak Mar 10 '13 at 16:54
  • Hi, thanks but it gives the following error: The method putIntegerArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, ArrayList>) It is an array inside another array – Ang3lo Mar 10 '13 at 16:55
  • @sajmon_d I have updated the answer and gave the link how can we pass the custom object between activities.. – Ajay S Mar 10 '13 at 17:10