将parcelable添加到自定义对象的接口类

时间:2015-02-08 10:32:51

标签: android object android-intent android-activity parcelable

我有一个自定义对象类但是通过接口实现,我如何在其中加入parceable。 我跟踪并搜索了parceable,但它只适用于对象类。 例如:How can I make my custom objects Parcelable?

我想将我的对象列表传递给android中的另一个活动。

代码:

public interface Projection {

  interface Job {
        @XBRead("./task")
        List<Task> getTasks();

        @XBRead("./id")
        String getid();

        @XBRead("./job_title")
        String getjob_title();

        @XBRead("./job_description")
        String getjob_description();

        @XBRead("./job_room")
        String getjob_room();

        @XBRead("./status")
        String getstatus();
    }

    interface Task {

        @XBRead("./task_id")
        String gettask_id();

        @XBRead("./task_title")
        String gettask_title();

        @XBRead("./task_description")
        String gettask_description();

        @XBRead("./task_status")
        String gettask_status();


    }

    @XBRead("/root/job")
    List<Job> getJobs();
}

2 个答案:

答案 0 :(得分:12)

您的自定义界面需要extend Parcelable

实现自定义界面的类还需要实现Parcelable界面,包括CREATOR

然后,您可以将实现自定义界面的对象添加到Intent,如下所示:

intent.putExtra("thing", thing);

或添加包含以下对象的ArrayList

ArrayList<Thing> things;
intent.putParcelableArrayListExtra("things", things);

在接收端,Activity可以像这样从Intent中提取对象:

Thing thing = intent.getParcelableExtra("thing");

ArrayList<Thing> things = intent.getParcelableArrayListExtra("things");

答案 1 :(得分:0)

完整代码如下

MyInterface

public interface MyInterface extends Parcelable {

}

MyImplementation1

public class MyImplementation1 implements MyInterface, Parcelable {
    public final int var1;

    public MyImplementation1() {
        this.var1 = 1;
    }

    protected MyImplementation1(Parcel in) {
        var1 = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(var1);
    }

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

    public static final Creator<MyImplementation1> CREATOR = new Creator<MyImplementation1>() {
        @Override
        public MyImplementation1 createFromParcel(Parcel in) {
            return new MyImplementation1(in);
        }

        @Override
        public MyImplementation1[] newArray(int size) {
            return new MyImplementation1[size];
        }
    };
}

MyImplementation2

public class MyImplementation2 implements MyInterface, Parcelable {
    public final int var2;

    public MyImplementation2(int var2) {
        this.var2 = var2;
    }

    protected MyImplementation2(Parcel in) {
        var2 = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(var2);
    }

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

    public static final Creator<MyImplementation2> CREATOR = new Creator<MyImplementation2>() {
        @Override
        public MyImplementation2 createFromParcel(Parcel in) {
            return new MyImplementation2(in);
        }

        @Override
        public MyImplementation2[] newArray(int size) {
            return new MyImplementation2[size];
        }
    };
}

MyClass

public class MyClass implements Parcelable {

    private final MyInterface var1;
    private final List<MyInterface> var2;


    public MyClass(MyInterface var1, List<MyInterface> var2) {
        this.var1 = var1;
        this.var2 = var2;
    }

    protected MyClass(Parcel in) {
        var1 = in.readParcelable(MyInterface.class.getClassLoader());
        var2 = in.readArrayList(MyInterface.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(var1, flags);
        dest.writeTypedList(var2);
    }

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

    public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
        @Override
        public MyClass createFromParcel(Parcel in) {
            return new MyClass(in);
        }

        @Override
        public MyClass[] newArray(int size) {
            return new MyClass[size];
        }
    };
}

使用

MainActivity1

public class MainActivity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_1);

        List<MyInterface> list = new ArrayList<>(2);
        list.add(new MyImplementation2(21));
        list.add(new MyImplementation2(22));

        MyClass myClass = new MyClass(new MyImplementation1(), list);

        Intent intent = new Intent(this, MainActivity2.class);
        Bundle b = new Bundle();
        b.putParcelable("key", myClass);
        intent.putExtras(b);
        startActivity(intent);
    }
}

MainActivity2

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_2);

        Bundle b = getIntent().getExtras();
        MyClass myClass = b.getParcelable("key");

    }
}