adding parcelable to an interface class for custom

2019-04-28 05:25发布

问题:

I have a custom object class but that is implemented through an inteface, how can i incorporate parceable in it. I have followed and searched about parceable, but it is only for object class. eg : How can I make my custom objects Parcelable?

I want to pass my object list to another activity in android.

code :

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();
}

回答1:

Your custom interfaces need to extend Parcelable.

Classes that implement your custom interface need to also implement the Parcelable interface, including the CREATOR.

You can then add an object implementing your custom interface to an Intent like this:

intent.putExtra("thing", thing);

or add an ArrayList containing these objects like this:

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

On the receiving end, the Activity can extract the objects from the Intent like this:

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

or

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


回答2:

Full code looks like

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];
        }
    };
}

Using it

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");

    }
}