9

I have a List in one of my activities and need to pass it to the next activity.

private List<Item> selectedData;  

I tried putting this in intent by :

intent.putExtra("selectedData", selectedData);  

But it is not working. What can be done?

5 Answers5

14

Like howettl mentioned in a comment, if you make the object you are keeping in your list serializeable then it become very easy. Then you can put it in a Bundle which you can then put in the intent. Here is an example:

class ExampleClass implements Serializable {
    public String toString() {
        return "I am a class";
    }
}

... */ Where you wanna create the activity /*

ExampleClass e = new ExampleClass();
ArrayList<ExampleClass> l = new ArrayList<>();
l.add(e);
Intent i = new Intent();
Bundle b = new Bundle();
b.putSerializeable(l);
i.putExtra("LIST", b);
startActivity(i);
hsson
  • 595
  • 1
  • 7
  • 16
  • 8
    Actually, it must be `b.putSerialzeable("key", l)` and it should be noted that to access your list in the other activity, you have to do `List listExtra = (ArrayList)( intent.getBundleExtra("LIST").getSerializable("key"))`. Still, its much easier than implementing Parceable for your ExampleClass. – Ridcully Dec 04 '15 at 07:34
11

You have to instantiate the List to a concrete type first. List itself is an interface.

If you implement the Parcelable interface in your object then you can use the putParcelableArrayListExtra() method to add it to the Intent.

Community
  • 1
  • 1
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • 1
    No, I tried it. Didn't work. I also tried **view_order_intent.putParcelableArrayListExtra("selectedData", (ArrayList extends Parcelable>) selectedData);**. There came out to be no errors but I am not able to get it. –  Jun 30 '11 at 21:26
  • **selectedData = bundle.getParcelableArrayList("selectedData");**. I tried this to get the code. I didn't work. –  Jun 30 '11 at 21:27
  • 1
    List is the parent of ArrayList.. The method specifically wants ArrayList.. Just change the type of List to ArrayList – thedjaney Aug 14 '13 at 02:22
  • `intent.putParcelableArrayListExtra("XXX", (ArrayList)yourList);` (assuming `Item` is `Parcelable`) – Martin Marconcini Apr 14 '17 at 18:50
4

i think ur item should be parcelable. and you should use arraylist instead of list. then use intent.putParcelableArrayListExtra

Nissan911
  • 293
  • 1
  • 7
  • 17
1

This is what worked for me.

//first create the list to put objects
private ArrayList<ItemCreate> itemsList = new ArrayList<>();

//on the sender activity
     //add items to list where necessary also make sure the Class model ItemCreate implements Serializable
     itemsList.add(theInstanceOfItemCreates);

        Intent goToActivity = new Intent(MainActivity.this, SecondActivity.class);
                        goToActivity.putExtra("ITEMS", itemsList);
                        startActivity(goToActivity);

    //then on second activity
    Intent i = getIntent();
            receivedItemsList = (ArrayList<ItemCreate>) i.getSerializableExtra("ITEMS");
            Log.d("Print Items Count", receivedItemsList.size()+"");
            for (Received item:
                 receivedItemList) {
                Log.d("Print Item name: ", item.getName() + "");
        }

I hope it works for you too.

Ronny K
  • 3,641
  • 4
  • 33
  • 43
0

Everyone says that you can use Serializable, but none mentioned that you can just cast the value to Serializable instead of list.

intent.putExtra("selectedData", (Serializable) selectedData);

Core's lists implementations already implement Serializable, so you're not bound to a specific implementation of list, but remember that you still can catch ClassCastException.

deathangel908
  • 8,601
  • 8
  • 47
  • 81