63

Possible Duplicate:
How to put a List in intent

I want to pass a List from one activity to another. So far I have not been successful. This is my code.

//desserts.java

private List<Item> data;  

@Override
public void onCreate(Bundle icicle) {
//Code
data.add(new Item(10, "dessert1"));
data.add(new Item(11, "dessert2"));
data.add(new Item(12, "dessert3"));
data.add(new Item(13, "dessert4"));
data.add(new Item(14, "dessert5"));
data.add(new Item(15, "dessert6"));
data.add(new Item(16, "dessert7"));
data.add(new Item(17, "dessert8"));
data.add(new Item(18, "dessert9"));
data.add(new Item(19, "dessert10"));
data.add(new Item(20, "dessert11"));  

//Some more code  
}  

@Override
public void onClick(View v) {  
Intent view_order_intent = new Intent(this, thirdpage.class);
view_order_intent.putExtra("data", data); 
startActivity(view_order_intent);  
}   

But I am not able to put data this way. I asked this question earlier but not much happened.
Kindly help. Also help me how to get data in next activity.

Community
  • 1
  • 1

4 Answers4

131

Assuming that your List is a list of strings make data an ArrayList<String> and use intent.putStringArrayListExtra("data", data)

Here is a skeleton of the code you need:

  1. Declare List

     private List<String> test;
    
  2. Init List at appropriate place

     test = new ArrayList<String>();
    

    and add data as appropriate to test.

  3. Pass to intent as follows:

     Intent intent = getIntent();  
     intent.putStringArrayListExtra("test", (ArrayList<String>) test);
    
  4. Retrieve data as follows:

     ArrayList<String> test = getIntent().getStringArrayListExtra("test");
    
starball
  • 20,030
  • 7
  • 43
  • 238
Mandel
  • 2,968
  • 2
  • 22
  • 19
  • You mean I should declare it as an ArrayList or initialize it as ArrayList? –  Jul 01 '11 at 05:21
  • 1
    But that way I am able to insert only one item in the list. I want two strings, which I am getting by item object. –  Jul 01 '11 at 06:01
  • I had not even noticed there were methods like putStringArrayListExtra and was getting to store my integer list as an int array. So, thank you! – likejudo May 13 '14 at 16:13
  • 1
    @Mandel What if it is a custom object? – Rafael Oct 12 '16 at 12:25
23

If you use ArrayList instead of list then also your problem wil be solved. In your code only modify List into ArrayList.

private List<Item> data;
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 3
    Later you can read the list by `ArrayList items = (ArrayList) getArguments().getSerializable(EXTRA_ITEMS);`. – CoolMind Sep 04 '17 at 10:00
12

you can do it in two ways using

  • Serializable

  • Parcelable.

This examle will show you how to implement it with serializable

class Customer implements Serializable
{
   // properties, getter setters & constructor
}

// This is your custom object
Customer customer = new Customer(name, address, zip);

Intent intent = new Intent();
intent.setClass(SourceActivity.this, TargetActivity.this);
intent.putExtra("customer", customer);
startActivity(intent);

// Now in your TargetActivity
Bundle extras = getIntent().getExtras();
if (extras != null)
{
    Customer customer = (Customer)extras.getSerializable("customer");
    // do something with the customer
}

Now have a look at this. This link will give you a brief overview of how to implement it with Parcelable.

Look at this.. This discussion will let you know which is much better way to implement it.

Thanks.

Community
  • 1
  • 1
N-JOY
  • 10,344
  • 7
  • 51
  • 69
  • 2
    This does not actually answer the question of, how to put a LIST inside an intent. Only on how to put a single specific item inside an intent. – Joakim Engstrom Jan 28 '16 at 10:37
  • @N-JOY do you have a sample code for `do something with customer`... how do I loop through the customer model and get info for each? Thanks a lot. – Woppi Feb 27 '17 at 09:53
-3
 //To send from the activity that is calling another activity via myIntent

    myIntent.putExtra("id","10");
    startActivity(myIntent);

    //To receive from another Activity

            Bundle bundle = getIntent().getExtras();
            String id=bundle.getString("id");
Rasel
  • 15,499
  • 6
  • 40
  • 50
  • 2
    How is it sending data which is a list? What is "10"? –  Jul 01 '11 at 05:19
  • id is the string you can retrieve from it.And 10 is the data you are sending.In the last line if you take the id you will get 10 that comes from the caller activiry – Rasel Jul 01 '11 at 05:23
  • 1
    Yes I tried this as I have written in my code. Didn't help. –  Jul 01 '11 at 05:26
  • There is no method of Bundle to receive list.So your code is not working.Use string array instead. – Rasel Jul 01 '11 at 05:29