android – Parceler解包给出错误的数据

android – Parceler解包给出错误的数据,第1张

概述我正在使用 parceler库来实现Parcelable接口. 我有这样的模特 @Parcel(Parcel.Serialization.BEAN)public class Ads {private Long id;private String title;private String description;private AdsType adsType;private String 我正在使用 parceler库来实现Parcelable接口.

我有这样的模特

@Parcel(Parcel.Serialization.BEAN)public class Ads {private Long ID;private String Title;private String description;private AdsType adsType;private String phone;private String email;private String city;private Long categoryID;private ArrayList<Integer> creationDate;//Todo remove transIEntprivate transIEnt ArrayList<Long> imageIDs;private transIEnt Long price;@Serializedname("adscategory")private Advcategory advcategory;public Ads() {}public Ads(String Title,String description,AdsType adsType,String        phone,String email,String city,Long categoryID,Long price,ArrayList<Long> imageIDs) {    this.Title = Title;    this.description = description;    this.adsType = adsType;    this.phone = phone;    this.email = email;    this.city = city;    this.categoryID = categoryID;    this.price = price;    this.imageIDs = imageIDs;}@ParcelConstructorpublic Ads(Long ID,String Title,String phone,ArrayList<Long>                   imageIDs,ArrayList<Integer> creationDate,Advcategory advcategory) {    this.ID = ID;    this.Title = Title;    this.description = description;    this.adsType = adsType;    this.phone = phone;    this.email = email;    this.city = city;    this.imageIDs = imageIDs;    this.price = price;    this.creationDate = creationDate;    this.advcategory = advcategory;}public Long getID() {    return ID;}public String getTitle() {    return Title;}public String getDescription() {    return description;}public AdsType getAdsType() {    return adsType;}public String getPhone() {    return phone;}public String getEmail() {    return email;}public String getCity() {    return city;}public Advcategory getAdvcategory() {    return advcategory;}public voID setAdvcategory(Advcategory advcategory) {    this.advcategory = advcategory;}public Long getcategoryID() {    return categoryID;}public ArrayList<Long> getimageIDs() {    return imageIDs;}public voID setimageIDs(ArrayList<Long> imageIDs) {    this.imageIDs = imageIDs;}public int getPrice() {    //Todo replace with real price    return new Random().nextInt(100000);}public voID setPrice(Long price) {    this.price = price;}public ArrayList<Integer> getCreationDate() {    return creationDate;}public voID setCreationDate(ArrayList<Integer> creationDate) {    this.creationDate = creationDate;}@OverrIDepublic boolean equals(Object o) {    if (this == o) return true;    if (o == null || getClass() != o.getClass()) return false;    Ads ads = (Ads) o;    return ID.equals(ads.ID);}@OverrIDepublic int hashCode() {    int result = ID.hashCode();    result = 31 * result + Title.hashCode();    result = 31 * result + description.hashCode();    result = 31 * result + adsType.hashCode();    result = 31 * result + (phone != null ? phone.hashCode() : 0);    result = 31 * result + (email != null ? email.hashCode() : 0);    result = 31 * result + (city != null ? city.hashCode() : 0);    result = 31 * result + advcategory.hashCode();    result = 31 * result + (categoryID != null ? categoryID.hashCode() : 0);    return result;}@OverrIDepublic String toString() {    return "Ads{" +            "ID=" + ID +            ",Title='" + Title + '\'' +            ",description='" + description + '\'' +            ",adsType=" + adsType +            ",phone='" + phone + '\'' +            ",email='" + email + '\'' +            ",city='" + city + '\'' +            ",creationDate='" + creationDate.toString() +            '}';}public static class List extends ArrayList<Ads> {}}

我正在包装我的模型并将其置于意图之中.

Intent adsDetailsIntent = new Intent(this,AdsDetailsActivity.class);    Bundle details = new Bundle();    Ads advertisement = mAdsAdapter.getItem(position);    details.putParcelable(AdsDetailsActivity.ADS_DETAILS,Parcels.wrap(advertisement));    Ads ads = Parcels.unwrap(details.getParcelable(AdsDetailsActivity.ADS_DETAILS));    Log.d("ads",ads.toString());    adsDetailsIntent.putExtras(details);    startActivity(adsDetailsIntent);

并在活动中解开

mAdsDetails = Parcels.unwrap(            (Parcelable) this.getIntent().getParcelableExtra(ADS_DETAILS));

但有时字段“creationDate”在展开活动后有错误的价值.

我尝试记录它,并在从Bundle打开后 – 没关系,但在活动中 – 它有奇怪的数据.

示例:

unwrap from bundle immediately after creating it

Ads{ID=16,Title=’MTitle’,description=’Mads’,adsType=BUY,phone=’+30890931231′,email=’+380932309046′,city=’Анабарский национальный улус’,creationDate='[2015,8,9,27,350946000]}

unwrap from activity intent.getExtra()

Ads{ID=null,Title=’null’,description=’null’,adsType=null,phone=’null’,email=’null’,city=’null’,creationDate='[8,350946000,null,

我不知道为什么,但是它创建了一个带有creationDate1大小的数组,并用零填充它.

解决方法 我注意到你在课堂上打开了

mAdsDetails = Parcels.unwrap(            (Parcelable) this.getIntent().getParcelableExtra(ADS_DETAILS));

你有没有尝试过这种方式:

mAdsDetails = Parcels.unwrap(this.getIntent().getExtras().get(ADS_DETAILS));
总结

以上是内存溢出为你收集整理的android – Parceler解包给出错误的数据全部内容,希望文章能够帮你解决android – Parceler解包给出错误的数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1123562.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存