[英]How to pass List<custom object> through activities
ArrayList<Item> arrayOfList;
I want to pass arrayOfList;
我想通过arrayOfList;
to next activity on Itemclick
of listview
到listview
Itemclick
的下一个活动
Tried Things 试过的东西
Intent sec = new Intent(this, IndividualPage.class);
Bundle b = new Bundle();
b.putParcelableArrayList("mylist", arrayOfList);
sec.putExtras(b);
To retrieve the arraylist 检索数组列表
Bundle b = this.getIntent().getExtras();
ArrayList<Item> cats = b.getParcelableArrayList("mylist");
System.out.println(cats);
But i am getting null
in console. 但是我在控制台中得到的是null
。
Is there any other efficient way to pass the data. 还有其他有效的方式来传递数据。
Item.java Item.java
public class Item implements Parcelable { 公共类Item实现了Parcelable {
private String Name;
private String Location;
private String Image;
private String Sector;
private int Founded;
private String Status;
private int RowVAls;
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getLocation() {
return Location;
}
public void setLocation(String Location) {
this.Location = Location;
}
public String getImage() {
//return "http://23.253.164.20:8099/"+Image;
return "http://23.253.164.20:8099/"+Image;
}
public void setImage(String Image) {
this.Image = Image;
}
public String getSector() {
return Sector;
}
public void setSector(String Sector) {
this.Sector = Sector;
}
public int getFounded() {
return Founded;
}
public void setFounded(int Founded) {
this.Founded = Founded;
}
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
public int getRowVAls() {
return RowVAls;
}
public void setRowVAls(int RowVAls) {
this.RowVAls = RowVAls;
}
protected Item(Parcel in) {
Name = in.readString();
Location = in.readString();
Image = in.readString();
Sector = in.readString();
Founded = in.readInt();
Status = in.readString();
RowVAls = in.readInt();
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>() {
@Override
public Item createFromParcel(Parcel in) {
return new Item(in);
}
@Override
public Item[] newArray(int size) {
return new Item[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
dest.writeString(Location);
dest.writeString(Image);
dest.writeString(Sector);
dest.writeInt(Founded);
dest.writeString(Status);
dest.writeInt(RowVAls);
}
Note:- Item is neither Parcelable
nor Serializable
. 注意:-物品既不可Parcelable
也不可Serializable
。 And i would not like to make any changes in that. 我不想对此进行任何更改。
Item is neither Parcelable nor Serializable . Item既不可打包也不可序列化。 And i would not like to make any changes in that. 我不想对此进行任何更改。
then you can't. 那你做不到 I would strongly recommend you to look into the Parcelable
interface, avoiding tricks like making the field public static
我强烈建议您查看Parcelable
界面, 避免使用诸如将字段设为public static
类的技巧
As others have said, you can't do it properly without implementing parcelable - which sometimes isn't possible. 就像其他人所说的,如果不实现可打包,您将无法正确执行-有时是不可能的。
You're only other option is to architect a way in which you can put your list in a static context so it's accessible from wherever. 您唯一的选择是设计一种方法,您可以将列表放在静态上下文中,以便可以从任何地方访问它。
You could create a singleton SparseArray and related integer, and pass around the integer between activities describing the position in the SparseArray. 您可以创建一个单例SparseArray和相关的整数,并在描述SparseArray中位置的活动之间传递整数。 You'd just have to ensure that you incremented your integer and removed the item from the SparseArray once you'd grabbed it. 您只需要确保递增整数并在抓住它后就将其从SparseArray中删除。 Off the top of my head - 从我的头顶上-
private class ComplexObjectPassing {
private static SparseArray<Object> sArray = new SparseArray<Object>();
private static int count = 0;
public static int putObject(Object obj) {
count++;
sArray.put(count, obj);
}
public static Object getObject(int index) {
Object obj = sArray.get(index);
sArray.put(index, null);
return obj;
}
}
Obviously, this would end with a huge memory leak when you passed things and they weren't pulled out by the new activity. 显然,当您传递事物时,这将以巨大的内存泄漏结束,并且新活动不会使它们退出。 All of the alternatives are going to have downsides and need to be carefully managed to reduce inevitable problems. 所有替代方案都有缺点,需要仔细管理以减少不可避免的问题。
You try the below 您尝试以下
intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
startActivity(intent);
Retrieve it 取回
getIntent().getParcelableArrayListExtra("key");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.