【问题标题】:How to make an Interface Parcelable?如何使接口可包裹?
【发布时间】:2015-01-08 12:44:01
【问题描述】:

如何使 Interface 对象 Parcelable?

public interface Options{
    public String getAction();
    public String getType();

}


public class OptionsInfo implements Parcelable {

    private int duration;
    private ArrayList<Options> path;

    public OptionsInfo(int duration) {
        super();
        this.duration = duration;
        this.path = new ArrayList<Options>();
    }

    public ArrayList<Options> getPath() {
        return path;
    }

    public void setPath(ArrayList<Options> path) {
        this.path = path;
    }

     public void addToPath(Options object)
        {
            path.add(object);
        }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeInt(this.duration);
        dest.writeTypedList(this.path);

    }

    private OptionsInfo (Parcel in){

        path = new ArrayList<Options>();

        this.duration = in.readInt();

        in.readTypedList(this.path, Options.CREATOR);
    }

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

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

}

【问题讨论】:

  • 你没有。接口不包含要打包的数据。如果您的问题是:“如何将我的接口实例持久化到两个或多个活动中?”尽管不鼓励这样做,但您可以通过静态引用来处理它。
  • 除了Options之外,所有path元素都必须实现Parcelable。如果您想强制执行并检查编译时间,请更改为public interface Options extends Parcelable。发布Options 的一些具体实现,我可以通过示例发布答案。
  • Parcelable 是数据。数据模型通常不实现接口,因为它们没有行为。

标签: android parcelable


【解决方案1】:
public interface Options implements Parcelable {
public String getAction();
public String getType();

Creator<Options> getCreator();
}


public class OptionsInfo implements Options{

private int duration;
private ArrayList<Options> path;

public OptionsInfo(int duration) {
    super();
    this.duration = duration;
    this.path = new ArrayList<Options>();
}

public ArrayList<Options> getPath() {
    return path;
}

public void setPath(ArrayList<Options> path) {
    this.path = path;
}

 public void addToPath(Options object)
    {
        path.add(object);
    }

public int getDuration() {
    return duration;
}

public void setDuration(int duration) {
    this.duration = duration;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(this.duration);
    dest.writeTypedList(this.path);

}

private OptionsInfo (Parcel in){

    path = new ArrayList<Options>();

    this.duration = in.readInt();

    in.readTypedList(this.path, Options.CREATOR);
}

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

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

@Override
public Creator<Options> getCreator() {
    return CREATOR;
}
}

【讨论】:

  • 这是错误的。如果您尝试在接口上实现接口,则会收到以下错误“接口不允许实现子句”。
  • 当从另一个接口实现接口时,你必须使用扩展而不是实现
猜你喜欢
  • 2018-12-11
  • 2014-07-30
  • 2014-05-03
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 2020-01-29
  • 2021-05-20
  • 2017-02-14
相关资源
最近更新 更多