14

I have the following code:

List<ValueActivity> list = new ArrayList<ValueActivity>();    
list = setList();    
Intent intent = new Intent(NOTIFICATION);    
Bundle bundle = new Bundle();
bundle.put ????("list", list);
intent.putExtra("bundle", bundle);
sendBroadcast(intent);

How to write line 5 and how getExtra in destination intent

Mysterion
  • 9,050
  • 3
  • 30
  • 52
user3016825
  • 143
  • 1
  • 1
  • 4
  • Is `ValueActivity` class is implementing serializable or parcelable interface ? – ρяσѕρєя K Jan 28 '15 at 11:19
  • what have you try with this line 5? – Mysterion Jan 28 '15 at 11:20
  • see here: http://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another – Opiatefuchs Jan 28 '15 at 11:21
  • http://stackoverflow.com/questions/6541088/how-to-put-a-list-in-intent – Neha Agarwal Jan 28 '15 at 11:21
  • another possible solution, if the list is not too long, create one String from the list, where everything from the list is put together and separated with a delimeter. Than You can pass a simple String and get the elements by String.split() – Opiatefuchs Jan 28 '15 at 11:23
  • public class ValueActivity { private int id; private String nameP, nameA; private int hourS; public String get_nameP() { return nameP; } public void set_nameP(String nameP) { this.nameP = nameP; } public String get_nameA() { return nameA; } public void set_nameA(String nameA) { this.nameA = nameA; } } – user3016825 Jan 28 '15 at 11:32
  • how to use put in line 5 – user3016825 Jan 28 '15 at 11:33

2 Answers2

28

You will need to have ValueActivity implement Parcelable interface and you will need to implement writeToParcel() the CREATOR and a suitable constructor that takes a Parcel as argument. See the documentation of Parcelable interface.

To put the list into the Bundle, use:

bundle.putParcelableArrayList("list", list);

To get the list out of the Bundle in the target activity, use:

List<ValueActivity> = listbundle.getParcelableArrayList("list");
Héctor
  • 24,444
  • 35
  • 132
  • 243
David Wasser
  • 93,459
  • 16
  • 209
  • 274
13

try to use :

String jsonList = gson.toJson(youList);
// Add String to bundle
KOTIOS
  • 11,177
  • 3
  • 39
  • 66