1

我正在尝试将一个类型的对象传递给我的应用程序Team中的另一个对象。Activity

Team班级:

public class Team implements Parcelable {

    String teamName;

    //Name and Link to competition of Team
    TreeMap<String, String> competitions;
    //Name of competition with a map of matchdays with all games to a matchday
    TreeMap<String, HashMap<Integer, ArrayList<Event>>> matchDays;

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(teamName);
        dest.writeMap(competitions);    
    }

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

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

    private Team(Parcel in) {
        teamName = in.readString();

        in.readMap(competitions, Team.class.getClassLoader());
    }
}

编组时出现 RuntimeException:

TreeMap<String, HashMap<Integer, ArrayList<Event>>> matchDays;

如何将嵌套的 TreeMap 与类的其余部分一起传递?

4

1 回答 1

1

String, TreeMap, 并且HashMap都实现了Serializable接口。您可能会考虑Serializable在您的Team类中实现并以这种方式在活动之间传递它。这样做可以使您可以直接从 加载对象,Bundle或者Intent无需手动解析它们。

public class Team implements Serializable {

    String teamName;

    //Name and Link to competition of Team
    TreeMap<String, String> competitions;
    //Name of competition with a map of matchdays with all games to a matchday
    TreeMap<String, HashMap<Integer, ArrayList<Event>>> matchDays;

不需要额外的解析代码。

(编辑:ArrayList也实现Serializable了,所以这个解决方案取决于Event类是否是可序列化的。)

于 2012-07-06T20:23:18.483 回答