java Java中的数组列表意图额外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15731029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array List Intent extra in Java
提问by starrystar
I am trying to skip my ArrayList
through Intent
. But I cannot find what to write in extra? Any get methods I am getting errors like "not applicable for string."
我想我要跳过ArrayList
通过Intent
。但是我找不到额外写什么?任何获取方法都会出现“不适用于字符串”之类的错误。
Item Details:
项目详情:
public class ItemDetails {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getImageNumber() {
return imageNumber;
}
public void setImageNumber(int imageNumber) {
this.imageNumber = imageNumber;
}
public int getVideoNumber() {
return videoNumber;
}
public void setVideoNumber(int videoNumber) {
this.videoNumber = videoNumber;
}
public void setChild(ArrayList<ItemDetails> item_child)
{
this.item_child = item_child;
}
public ArrayList<ItemDetails> getChild()
{
return this.item_child;
}
public void setParent(ArrayList<ItemDetails> item_parent)
{
this.item_parent = item_parent;
}
public ArrayList<ItemDetails> getParent()
{
return this.item_parent;
}
private String name ;
private String itemDescription;
private String price;
private int imageNumber;
private int videoNumber;
private ArrayList<ItemDetails> item_child;
private ArrayList<ItemDetails> item_parent;
}
My class:
我的课:
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
ImageView itemImage;
ArrayList<ItemDetails> item_parent;
ArrayList<ItemDetails> item_child;
}
My putExtra:
我的 putExtra:
intObj.putExtra("exerciselist",obj_itemDetails.getChild());
GetChild function :
获取子函数:
public ArrayList<ItemDetails> getChild()
{
return this.item_child;
}
But I cannot find how am I supposed to write to get arrayList?
但是我找不到我应该如何写来获取arrayList?
ArrayList<ItemDetails> child1 = getIntent().?????????
回答by Simon Dorociak
But i cannot find how am ? supposed to write to get arrayList
但我找不到怎么样?应该写入以获取 arrayList
Short answer: you can't. You can only pass ArrayList<String>
and then retrieve it with
简短的回答:你不能。您只能通过ArrayList<String>
然后检索它
getIntent().getStringArrayListExtra("key");
But. If you want to pass custom objects via Intent your objects have to implement:
但是。如果您想通过 Intent 传递自定义对象,您的对象必须实现:
You can choose one of them. Both works same but have different implementations.
您可以选择其中之一。两者的工作原理相同,但实现方式不同。
Parcelable interface:
可打包接口:
If you choose Parcelable interface, your ItemDetails
class have to implement Parcelable. Then you can put it as
如果你选择 Parcelable 接口,你的ItemDetails
类必须实现 Parcelable。然后你可以把它作为
intent.putParcelableArrayListExtra("key", value);
and retrieve it as:
并将其检索为:
getIntent().getParcelableArrayListExtra("key");
I won't write you Parcelable implementation because it requires a little more code. Here is nice example.
我不会为您编写 Parcelable 实现,因为它需要更多代码。这是很好的例子。
Serializable interface:
可序列化接口:
If you choose Serializable interface i suggest you to create class named for instance ItemDetailsWrapper that will wrap your ArrayList(s)<ItemDetails>
如果您选择 Serializable 接口,我建议您创建名为 ItemDetailsWrapper 的类,它将包装您的 ArrayList(s)<ItemDetails>
Both i.e. ItemDetailsWrapper and ItemDetails class have to implement Serializable interface. Now you are able to pass it via Intent like this:
即 ItemDetailsWrapper 和 ItemDetails 类都必须实现 Serializable 接口。现在你可以像这样通过 Intent 传递它:
getIntent().putExtra("key", <serializableClass>); // storing
getIntent().getSerializableExtra("key"); // retrieving
Example of implementation:
实施例:
public class ItemDetailsWrapper implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<ItemDetails> itemDetails;
public ItemDetailsWrapper(ArrayList<ItemDetails> items) {
this.itemDetails = items;
}
public ArrayList<ItemDetails> getItemDetails() {
return itemDetails;
}
}
public class ItemDetails implements Serializable {
private static final long serialVersionUID = 1L;
// getters, setters and properties
}
And how to pass through Activities:
以及如何通过活动:
ItemDetailsWrapper wrapper = new ItemDetailsWrapper(list);
Intent i = new Intent(<context>, <targetActivity>);
i.putExtra("obj", wrapper); // i.putExtra("obj", new ItemDetailsWrapper(list));
// retrieving
ItemDetailsWrapper wrap =
(ItemDetailsWrapper) getIntent().getSerializableExtra("obj");
ArrayList<ItemDetails> list = wrap.getItemDetails();