Advertisement
Guest User

Parceable Android object to activity transfer data in betwee

a guest
Nov 21st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. Parceable object passing Class  object list in android
  2.  
  3.  
  4. public class OrderedData implements Parcelable
  5. {
  6.  
  7.     private String _title, _subtitle, _person_type, _processed_name;
  8.     private Integer _price, _quantity;
  9.  
  10.     public OrderedData(String title, String subtitle, String person_type, String processed_name, Integer price, Integer quantity)
  11.     {
  12.         _title = title;
  13.         _subtitle = subtitle;
  14.         _person_type = person_type;
  15.         _processed_name = processed_name;
  16.         _price = price;
  17.         _quantity = quantity;
  18.     }
  19.  
  20.     public OrderedData(Parcel in)
  21.     {
  22.         // TODO Auto-generated constructor stub
  23.  
  24.         String[] data = new String[6];
  25.  
  26.         in.readStringArray(data);
  27.         _title = data[0];
  28.         _subtitle = data[1];
  29.         _person_type = data[2];
  30.         _processed_name = data[3];
  31.         _price = Integer.parseInt(data[4]);
  32.         _quantity = Integer.parseInt(data[5]);
  33.     }
  34.  
  35.     public String getTitle()
  36.     {
  37.         return _title;
  38.     }
  39.  
  40.     public String getSubTitle()
  41.     {
  42.         return _subtitle;
  43.     }
  44.  
  45.     public String getPersonType()
  46.     {
  47.         return _person_type;
  48.     }
  49.  
  50.     public String getProcessedName()
  51.     {
  52.         return _processed_name;
  53.     }
  54.  
  55.     public Integer getPrice()
  56.     {
  57.         return _price;
  58.     }
  59.  
  60.     public Integer getQuantity()
  61.     {
  62.         return _quantity;
  63.     }
  64.  
  65.     @Override
  66.     public int describeContents()
  67.     {
  68.         // TODO Auto-generated method stub
  69.         return 0;
  70.     }
  71.  
  72.     @Override
  73.     public void writeToParcel(Parcel dest, int flags)
  74.     {
  75.         dest.writeStringArray(new String[]
  76.         { this._title, this._subtitle, this._person_type, this._processed_name, String.valueOf(this._price), String.valueOf(this._quantity) });
  77.     }
  78.  
  79.     public static final Parcelable.Creator<OrderedData> CREATOR = new Parcelable.Creator<OrderedData>()
  80.     {
  81.         public OrderedData createFromParcel(Parcel in)
  82.         {
  83.  
  84.             return new OrderedData(in);
  85.         }
  86.  
  87.         public OrderedData[] newArray(int size)
  88.         {
  89.             return new OrderedData[size];
  90.         }
  91.     };
  92. }
  93.  
  94.  
  95. FirstActivity Android
  96.  
  97.  
  98. ArrayList<OrderedData> list = new ArrayList<OrderedData>();
  99.  
  100.         list.add(new OrderedData("abc", "abc", "abc", "abc", 1, 1));
  101.         list.add(new OrderedData("abc", "abc", "abc", "abc", 1, 1));
  102.         list.add(new OrderedData("abc", "abc", "abc", "abc", 1, 1));
  103.  
  104.         Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  105.         intent.putParcelableArrayListExtra("data", list);
  106.         startActivity(intent);
  107.  
  108.  
  109.  
  110. SecondActivity Android
  111.  
  112.  
  113.         List<OrderedData> list=getIntent().getParcelableArrayListExtra("data");
  114.         int a=10;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement