2017-08-09 113 views
0

在我的課程中的一個項目我使用Parcelable,我試圖讀寫類對象,我找不到任何解決方案,我該如何做到這一點,例如:android Parcelable寫類對象

public static class ListsBean implements Parcelable { 
    private UserDailyListBean UserDailyList; 
    private List<FoodsBean> foods; 

    private ListsBean(Parcel in) { 
     in.readList(new ArrayList<FoodsBean>(), null); 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeList(foods); 
    } 

    public static class UserDailyListBean { 
     private String id; 
     private String diet_visit_id; 
    } 

    public static class FoodsBean { 
     private String id; 
     private String meal; 
     private String food; 
     ... 
    } 
} 
這個類

我有一個領域,如UserDailyListBean和它不是像List室內用FoodsBean,怎麼能讀,寫的?

回答

0

可以使UserDailyListBean實現Parcelable和做這樣的事情

public class ListsBean implements Parcelable { 
    private UserDailyListBean UserDailyList; 
    private List<FoodsBean> foods; 

    public ListsBean() {} 

    public static class UserDailyListBean implements Parcelable { 
     private String id; 
     private String diet_visit_id; 

     public UserDailyListBean() {} 
     @Override 
     public int describeContents() { 
      return 0; 
     } 

     @Override 
     public void writeToParcel(Parcel dest, int flags) { 
      dest.writeString(this.id); 
      dest.writeString(this.diet_visit_id); 
     } 



     protected UserDailyListBean(Parcel in) { 
      this.id = in.readString(); 
      this.diet_visit_id = in.readString(); 
     } 

     public static final Creator<UserDailyListBean> CREATOR = new Creator<UserDailyListBean>() { 
      @Override 
      public UserDailyListBean createFromParcel(Parcel source) { 
       return new UserDailyListBean(source); 
      } 

      @Override 
      public UserDailyListBean[] newArray(int size) { 
       return new UserDailyListBean[size]; 
      } 
     }; 
    } 

    public static class FoodsBean { 
     private String id; 
     private String meal; 
     private String food; 

    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeParcelable(this.UserDailyList, flags); 
     dest.writeList(this.foods); 
    } 

    protected ListsBean(Parcel in) { 
     this.UserDailyList = in.readParcelable(UserDailyListBean.class.getClassLoader()); 
     this.foods = new ArrayList<FoodsBean>(); 
     in.readList(this.foods, FoodsBean.class.getClassLoader()); 
    } 

    public static final Creator<ListsBean> CREATOR = new Creator<ListsBean>() { 
     @Override 
     public ListsBean createFromParcel(Parcel source) { 
      return new ListsBean(source); 
     } 

     @Override 
     public ListsBean[] newArray(int size) { 
      return new ListsBean[size]; 
     } 
    }; 
} 
0

搜索一些教程網站我的問題解決了通過this link

1

使用ParceableGenerator插件爲Android Studio之後。當你在對象中使用Parceable時,對象類的類型變量必須實現Parceable。 這個插件對我來說非常有用和簡單

+0

非常感謝,我打算安裝 –