Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. //MyObjects Parcelable class 
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.os.Parcel;
  6. import android.os.Parcelable;
  7.  
  8. public class MyObjects implements Parcelable {
  9.  
  10. private int age;
  11. private String name;
  12.  
  13. private ArrayList<String> address;
  14.  
  15. public MyObjects(String name, int age, ArrayList<String> address) {
  16. this.name = name;
  17. this.age = age;
  18. this.address = address;
  19.  
  20.  
  21. public MyObjects(Parcel source) {
  22. age = source.readInt();
  23. name = source.readString();
  24. address = source.createStringArrayList();
  25.  
  26. @Override 
  27. public int describeContents() { 
  28. return 0; 
  29.  
  30. @Override 
  31. public void writeToParcel(Parcel dest, int flags) {
  32. dest.writeInt(age);
  33. dest.writeString(name);
  34. dest.writeStringList(address);
  35.  
  36.  
  37. public int getAge() { 
  38. return age;
  39.  
  40. public String getName() {
  41. return name;
  42.  
  43. public ArrayList<String> getAddress() {
  44. if (!(address == null))
  45. return address;
  46. else 
  47. return new ArrayList<String>();
  48.  
  49. public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
  50. @Override 
  51. public MyObjects[] newArray(int size) { 
  52. return new MyObjects[size]; 
  53.  
  54. @Override 
  55. public MyObjects createFromParcel(Parcel source) { 
  56. return new MyObjects(source); 
  57. }; 
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. MyObjects mObjects = new MyObjects("name","age","Address array here");
  65.  
  66. //Passing MyOjects instance 
  67. Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
  68. mIntent.putExtra("UniqueKey", mObjects); 
  69. startActivity(mIntent); 
  70.  
  71.  
  72. //Getting MyObjects instance 
  73. Intent mIntent = getIntent(); 
  74. MyObjects workorder = (MyObjects) mIntent.getParcelable("UniqueKey"); 
  75.  
  76.  
  77.  
  78.  
  79. //You can pass Arraylist of Parceble obect as below 
  80.  
  81. //Array of MyObjects 
  82. ArrayList<MyObjects> mUsers; 
  83.  
  84. //Passing MyOjects instance 
  85. Intent mIntent = new Intent(FromActivity.this, ToActivity.class); 
  86. mIntent.putParcelableArrayListExtra("UniqueKey", mUsers); 
  87. startActivity(mIntent); 
  88.  
  89.  
  90. //Getting MyObjects instance 
  91. Intent mIntent = getIntent(); 
  92. ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey"); 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement