Guest User

ParcelableExample

a guest
Jun 15th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. public class Notebook implements Parcelable{
  2.     // Constants   
  3.     private final String TAG = "Notebook";
  4.        
  5.     // Data
  6.     private long mLocalId;
  7.     private long mRemoteId;
  8.     private long mUserId;
  9.     private ArrayList<Note> mItems;
  10.     private long mCreateDate;
  11.     private String mTitle; 
  12.    
  13.     public Notebook() {
  14.         mLocalId = -1;
  15.         mRemoteId = -1;
  16.         mUserId = -1;
  17.         mItems = new ArrayList<Note>();
  18.         mCreateDate = AppUtils.now();
  19.         mTitle = "Default notebook";
  20.     }
  21.    
  22.     public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
  23.         public Notebook createFromParcel(Parcel in) {
  24.             return new Notebook(in);
  25.         }
  26.  
  27.         public Notebook[] newArray(int size) {
  28.             return new Notebook[size];
  29.         }
  30.     }; 
  31.    
  32.     public long getLocalId(){
  33.         return mLocalId;
  34.     }
  35.    
  36.     public Notebook setLocalId(long pLocalId){
  37.         mLocalId = pLocalId;
  38.         return this;
  39.     }
  40.    
  41.     public long getRemoteId(){
  42.         return mRemoteId;
  43.     }
  44.    
  45.     public Notebook setRemoteId(long pRemoteId){
  46.         mRemoteId = pRemoteId;
  47.         return this;
  48.     }
  49.    
  50.     public long getUserId(){
  51.         return mUserId;
  52.     }
  53.    
  54.     public Notebook setUserId(long pUserId){
  55.         mUserId = pUserId;
  56.         return this;
  57.     }
  58.    
  59.     public long getCreateDate(){
  60.         return mCreateDate;
  61.     }
  62.    
  63.     public Notebook setCreateDate(long pCreateDate){
  64.         mCreateDate = pCreateDate;
  65.         return this;
  66.     }
  67.    
  68.     public Notebook addItem(Note pItem){
  69.         mItems.add(pItem);
  70.         return this;
  71.     }
  72.    
  73.     public Note removeItemAt(int position){
  74.         try{
  75.             return mItems.remove(position);
  76.         }catch (IndexOutOfBoundsException e) {
  77.             e.printStackTrace();
  78.             return null;
  79.         }
  80.     }
  81.    
  82.     public Note getItem(int position){
  83.         try{
  84.             return mItems.get(position);
  85.         }catch (IndexOutOfBoundsException e) {
  86.             e.printStackTrace();
  87.             return null;
  88.         }
  89.     }
  90.    
  91.     public int getItemsCount(){
  92.         return mItems.size();
  93.     }
  94.    
  95.     public String getTitle(){
  96.         return mTitle;
  97.     }
  98.    
  99.     public Notebook setTitle(String pTitle){
  100.         mTitle = pTitle;
  101.         return this;
  102.     }
  103.    
  104.     @Override
  105.     public String toString() {
  106.         return Convertor.toString(this) + " total notes: " + getItemsCount();
  107.     }
  108.  
  109.     public Notebook setItems(ArrayList<Note> pNotes) {
  110.         mItems = pNotes;
  111.         return this;
  112.     }
  113.  
  114.     @Override
  115.     public int describeContents() {
  116.         return hashCode();
  117.     }
  118.  
  119.     @Override
  120.     public void writeToParcel(Parcel dest, int flags) {
  121.           Console.debug(TAG, "writing parcel... " + flags, Console.Liviu);           
  122.           dest.writeLong(mLocalId);
  123.           dest.writeLong(mRemoteId);
  124.           dest.writeLong(mUserId);
  125.           dest.writeLong(mCreateDate);
  126.           dest.writeString(mTitle);
  127.     }
  128.    
  129.       public Notebook(Parcel source){
  130.           /*
  131.            * Reconstruct from the Parcel
  132.            */
  133.           Console.debug(TAG, "ParcelData(Parcel source): time to put back parcel data", Console.Liviu);
  134.           mLocalId = source.readLong();
  135.           mRemoteId = source.readLong();
  136.           mUserId = source.readLong();
  137.           mCreateDate = source.readLong();
  138.           mTitle = source.readString();
  139.           mItems = new ArrayList<Note>();
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment