【问题标题】:ArrayList and ParcelableArrayList 和 Parcelable
【发布时间】:2017-07-27 10:40:26
【问题描述】:

我需要将自定义类的 ArrayList 从一个实体“转移”到另一个实体。我知道我需要实现 Parcelable 接口。

这是我的自定义类。

public class cSportsList implements Parcelable {
    boolean checked;
    String name;

    cSportsList(boolean check, String name_) {
        checked = check;
        name = name_;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //Non posso passare un booleano e non posso fare il cast boolean -> int
        if (checked) {
            dest.writeInt(1);
        }
        else dest.writeInt(0);
        dest.writeString(name);
    }

    public static final Parcelable.Creator<cSportsList> CREATOR = new Parcelable.Creator<cSportsList>() {
        public cSportsList createFromParcel(Parcel in) {
            return new cSportsList(in);
        }

        public cSportsList[] newArray(int size) {
            return new cSportsList[size];
        }
    };

    private cSportsList(Parcel in) {
        if (in.readInt() == 1) {
            checked = true;
        }
        else {
            checked = false;
        }
        name = in.readString();


    }
}

这是实体“来自”中的代码

//This is sportsMap: ArrayList<cSportsList> sportsMap = new ArrayList<cSportsList>();
Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", (ArrayList<? extends Parcelable>)  sportsMap);  //I have tried with ArrayList<cSportsList> too.
this.startActivity(intent);

这是实体“to”中的代码

final Intent srcIntent = getIntent();
ArrayList<cSportsList> sportsMap = srcIntent.getParcelableExtra("sportsMap");

问题是:在实体“To”中,sportsMap 为空。 如果我在“writeToParcel”和“cSportsList(Parcelable in)”函数中设置“断点”,我会看到这两个函数都执行了代码。

我的错误在哪里?

谢谢。 M.

【问题讨论】:

  • getParcelableExtra 替换为 getParcelableArrayListExtra

标签: android arraylist parcelable


【解决方案1】:

尝试传递 Arraylist 而不是像这样强制转换:

Intent intent = new Intent(getApplicationContext(),WhatSportActivity.class);
intent.putParcelableArrayListExtra("sportsMap", sportsMap);
        startActivity(intent);

在 WhatSportActivity 中,

final Intent srcIntent = getIntent();
ArrayList<cSportsList> sportsMap = srcIntent.getParcelableExtra("sportsMap");

如果您仍然有问题,请尝试使用“Parceable”插件来实现 Parceable,因为它减少了使用 Parceable 列表时的错误。

【讨论】:

    【解决方案2】:

    使用下面的代码

    sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");

    【讨论】:

      【解决方案3】:

      您也可以使用序列化。

      public class cSportsList implements Serializable

      用于放置:

      intent.putExtra("LIST",list);

      获取:

      (ArrayList&lt;&gt;)intent.getSerializableExtra("LIST");

      【讨论】:

      • 我读到 Parcelable 比 Serialize 更好。但是:谢谢。
      【解决方案4】:

      阅读时需要用到

      srcIntent.getParcelableArrayListExtra("sportsMap");
      

      在下面的代码中使用 Intent

      intent.putParcelableArrayListExtra("sportsMap", sportsMap);
      

      要从意图中读取它,请使用

      ArrayList<cSportsList> sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");
      

      Pass ArrayList<? implements Parcelable> to Activity阅读类似的解决方案

      【讨论】:

        【解决方案5】:

        你应该使用:

        ArrayList<cSportsList> sportsMap = srcIntent.getParcelableArrayListExtra("sportsMap");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-22
          • 2019-07-30
          • 2011-10-25
          • 2014-06-26
          • 1970-01-01
          相关资源
          最近更新 更多