相关疑难解决方法(0)

如何将List <>写入parcel

public class Category implements Parcelable {

    private int mCategoryId;
    private List<Video> mCategoryVideos;

 public int getCategoryId() {
        return mCategoryId;
    }

    public void setCategoryId(int mCategoryId) {
        this.mCategoryId = mCategoryId;
    }

 public List<Video> getCategoryVideos() {
        return mCategoryVideos;
    }

    public void setCategoryVideos(List<Video> videoList) {
        mCategoryVideos = videoList;
    }

@Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(mCategoryId);
        parcel.writeTypedList(mCategoryVideos);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Category createFromParcel(Parcel parcel) {
            final Category category = new Category();

            category.setCategoryId(parcel.readInt());
            category.setCategoryVideos(parcel.readTypedList()); */// **WHAT SHOULD …
Run Code Online (Sandbox Code Playgroud)

android

21
推荐指数
2
解决办法
2万
查看次数

Android:如何将Parcelable实现到我的对象?

我有这个包含我的JSON索引的ConstantData类,我需要在带有额外内容的活动之间传递它们.但在此之前,我必须首先将Parcelable实现到此类的对象.

我的问题是,如何在我的类中声明对象并将每个对象放在变量中?

我是新手,我现在完全无能为力.谢谢.随意修改我的代码如下.

ConstantData.java

public class ConstantData{

       public static String project_title = "project title";
       public static String organization_title = "organization title";
       public static String keyword = "keyword";
       public static String short_code = "short code";
       public static String project_description = "description";
       public static String smallImageUrl = "smallImageUrl";
       public static String bigImageUrl = "bigImageUrl";
       public static String price= "price";
       public static String country= "country";



        public static ArrayList<Project> projectsList = new ArrayList<Project>();


        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel out, …
Run Code Online (Sandbox Code Playgroud)

java android

16
推荐指数
1
解决办法
3万
查看次数

扩展实现Parcelable的类

我有一个类,我们称之为A类,它实现了Parcelable.

我有第二堂课,我们称之为B班,扩展到A班.

我的问题是:

如何将B类成员变量写入Parcel,然后将其父类(即:A类)成员变量写入Parcel(并随后将其读入)?

是否有一些漂亮的技巧,不需要重写A类的包裹代码?或者我只需要在A类中重写Parcel代码并为B类成员变量添加其他代码?

android parcelable

13
推荐指数
1
解决办法
5321
查看次数

parcelable继承:抽象类 - 哪个CREATOR?

我有一个A实现Parcelable 的抽象类.

我有一个班级B和一个班级C都延伸A.我怎样才能使它们变得可以分辨?

因为我可以链接它并在许多帖子中提供CREATORin ABlike之类的建议.但由于我有其他对象存储A-B-C类并实现Parcelable自己,这种方法似乎不起作用,因为当我想传递一个ArrayList时,A我将不得不CREATOR在类型列表中使用

ArrayList<A> elements = new ArrayList<>();
in.readTypedList(elements , B.CREATOR); // B.CREATOR? C.CREATOR???
Run Code Online (Sandbox Code Playgroud)

这显然没有意义.那么我怎样才能正确制作A Parcelable?

即我想使这个类Parcelable所以我可以以一种常见的方式引用A.

一个)

public abstract class A implements Parcelable {

    final String globalVar;

    public A(String globalVar) {
        this.globalVar = globalVar;
    }
}
Run Code Online (Sandbox Code Playgroud)

B)

public class B extends A {

    String bVar;


    public B(String global, String bVar) {
        super(global);
        this.bVar = bVar;
    }

    private B(Parcel …
Run Code Online (Sandbox Code Playgroud)

java inheritance android parcelable

8
推荐指数
2
解决办法
2269
查看次数

Android - 多态性和 Parcelable

从包裹中写入/读取时,我无法弄清楚如何使用多态性。我知道我需要在基类以及所有派生类中实现 Parcelable(在子类具有我想写入包裹的附加属性的情况下)。我不明白并且我不知道它是否可能是 - 如何从 Parcelable 读取子类,即,我怎么知道我正在从 Parcel 读取什么样的子类。我可以做一些 hack,比如在包中写入一些指示符,它会告诉我要使用什么类加载器,但我认为会有一些更优雅的方式,否则就没有太多的多态性使用。

为了说明我的问题,假设我有这样的课程:

形状类

public class Shape implements Parcelable {
public float area;

public Shape() {
}

public Shape(Parcel in) {
    area = in.readFloat();
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeFloat(area);
    }
//CREATOR etc..
}
Run Code Online (Sandbox Code Playgroud)

矩形形状类

 public class RectangleShape extends Shape {

    float a;
    float b;

    public RectangleShape(Parcel in) {
        super(in);
        a = in.readFloat();
        b = in.readFloat();
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeFloat(area);
        dest.writeFloat(a);
        dest.writeFloat(b);
    }

//CREATOR etc.. …
Run Code Online (Sandbox Code Playgroud)

java polymorphism android parcelable

5
推荐指数
1
解决办法
903
查看次数

Android抽象parcelable类

问题描述

我有Category实现的类Parcelable,我还有更多的类从Category类扩展.我的基类有2个protected成员title,id主要来自继承的类.因此,为了不在Parcelable继承的类中的任何地方实现相关的东西,我决定在基类中执行它并让它处理所有操作.

问题是我不能拥有Category类的构造函数,因为它是抽象类.那么解决方案是什么?由于我在类中有抽象方法,因此无法删除抽象修饰符.

源代码

public abstract class Category implements Parcelable {
    private static Map<Integer, Category> categoryMap = new TreeMap<Integer, Category>();

    protected Sting title;
    protected Integer  id;

    static {
        categoryMap.put(0, new Taxi());
        categoryMap.put(1, new Hotel());
    }

    private Category(Parcel in) {
        this.id = in.readInt();
        this.title = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInteger(id); …
Run Code Online (Sandbox Code Playgroud)

java android abstract-class parcelable

1
推荐指数
1
解决办法
4872
查看次数