将 parcelable 添加到自定义对象的接口 class
adding parcelable to an interface class for custom objects
我有一个自定义对象 class 但它是通过接口实现的,我如何将 parceable 合并到其中。
我关注并搜索了 parceable,但它仅适用于对象 class。
例如:How can I make my custom objects Parcelable?
我想将我的对象列表传递给 android 中的另一个 activity。
代码:
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();
}
您的自定义界面需要 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");
完整代码如下
我的界面
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];
}
};
}
我的班级
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];
}
};
}
正在使用
主要活动 1
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");
}
}
我有一个自定义对象 class 但它是通过接口实现的,我如何将 parceable 合并到其中。 我关注并搜索了 parceable,但它仅适用于对象 class。 例如:How can I make my custom objects Parcelable?
我想将我的对象列表传递给 android 中的另一个 activity。
代码:
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();
}
您的自定义界面需要 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");
完整代码如下
我的界面
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];
}
};
}
我的班级
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];
}
};
}
正在使用
主要活动 1
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");
}
}