在 Android 中创建自定义 java 对象 Parcelable
我正在尝试通过意图将 MyCustomObject
类型的对象发送到另一个活动。我知道,要使类 Parcelable,我应该执行 public class MyCustomObject Implements Parcelable
,但我不确定自定义对象数组如何在 Parcelable 中工作。这是我到目前为止所得到的。另外,我是否也需要使 Search 类实现 Parcelable ?
这是我更新的答案现在,当我执行intent.getParcelableExtra(search)
时,我会得到一个空对象。也许我没有正确创建搜索数组?
public class MyCustomObject implements Parcelable{
public Search search[];
public MyCustomObject(Parcel in){
in.readArray(MyCustomObject.class.getClassLoader());
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
dest.writeArray(search)
}
public static final Parcelable.Creator<MyCustomObject> CREATOR = new Parcelable.Creator<MyCustomObject>(){
@Override
public MyCustomObject createFromParcel(Parcel source){
return new MyCustomObject(source);
}
@Override
public MyCustomObject[] newArray(int size){
return new MyCustomObject[size];
}
}
public static class Search implements Parcelable{
public int rank;
public String title;
public String[] imageURL;
public Search(Parcel in){
rank = in.readInt();
title = in.readString();
String[] url = new String[4];
in.readStringArray(url);
url = imageURL
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
dest.writeInt(rank);
dest.writeString(title);
dest.writeStringArray(imageURL);
}
public static final Parcelable.Creator<Search> CREATOR = new Parcelable.Creator<Search>(){
@Override
public Search createFromParcel(Parcel source){
return new Search(source);
}
@Override
public Search[] newArray(int size){
return new Search[size];
}
}
}
}
I am trying to send an object of type MyCustomObject
to another activity via an intent. I know, to make the class Parcelable, I should do public class MyCustomObject implements Parcelable
, but I am not sure how custom object arrays work in parcelable. Here's what I have got so far.. Also, do I need to make the Search class implements Parcelable too?
Here is my updated answer I now get a null object when I do intent.getParcelableExtra(search)
. Maybe I am not creating the search array properly?
public class MyCustomObject implements Parcelable{
public Search search[];
public MyCustomObject(Parcel in){
in.readArray(MyCustomObject.class.getClassLoader());
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
dest.writeArray(search)
}
public static final Parcelable.Creator<MyCustomObject> CREATOR = new Parcelable.Creator<MyCustomObject>(){
@Override
public MyCustomObject createFromParcel(Parcel source){
return new MyCustomObject(source);
}
@Override
public MyCustomObject[] newArray(int size){
return new MyCustomObject[size];
}
}
public static class Search implements Parcelable{
public int rank;
public String title;
public String[] imageURL;
public Search(Parcel in){
rank = in.readInt();
title = in.readString();
String[] url = new String[4];
in.readStringArray(url);
url = imageURL
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
dest.writeInt(rank);
dest.writeString(title);
dest.writeStringArray(imageURL);
}
public static final Parcelable.Creator<Search> CREATOR = new Parcelable.Creator<Search>(){
@Override
public Search createFromParcel(Parcel source){
return new Search(source);
}
@Override
public Search[] newArray(int size){
return new Search[size];
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事。首先,您的 Search 对象还需要实现 Parcelable。接下来,您需要创建一个名为 CREATOR 的静态字段,该字段实现 Parcelable.Creator。这将有一个名为 createFromParcel 的方法,该方法返回 MyCustomObject 的实例。您可以在此处从包中读取数据并创建 MyCustomObject 的新实例。这本质上与 writeToParcel 相反。这一切也适用于搜索,因为您必须使其实现 Parcelable。总结:
A couple of things. First, your Search object will need to implement Parcelable as well. Next, you need to create a static field called CREATOR that implements Parcelable.Creator<MyCustomObject>. This will have a method called createFromParcel that returns an instance of MyCustomObject. That is where you read data out of a parcel and create a new instance of MyCustomObject. This is essentially the opposite of writeToParcel. This all applies to Search as well since you must make it implement Parcelable. In summary:
当您从 Parcel 读取Array 时,您想要读取 Search[] 数组。不是 MyCustomObject。
when you readArray from Parcel, you want to read a Search[] array. Not the MyCustomObject.