11

我试图跳过我ArrayListIntent。但是我找不到要写的额外内容?任何获取方法我都会收到诸如“不适用于字符串”之类的错误。

项目详情:

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

我的课:

static class ViewHolder {
    TextView txt_itemName;
    TextView txt_itemDescription;
    TextView txt_itemPrice;
    ImageView itemImage;
    ArrayList<ItemDetails> item_parent;
    ArrayList<ItemDetails> item_child;
}

我的putExtra:

intObj.putExtra("exerciselist",obj_itemDetails.getChild());

GetChild 函数:

public ArrayList<ItemDetails> getChild()
{
   return this.item_child;
}

但我找不到我应该如何写来获取arrayList?

ArrayList<ItemDetails> child1 = getIntent().?????????
4

1 回答 1

28

但我找不到我应该如何写来获取arrayList

简短的回答:你不能。你只能通过ArrayList<String>然后检索它

getIntent().getStringArrayListExtra("key");


但是。如果您想通过 Intent 传递自定义对象,您的对象必须实现:

您可以选择其中之一。两者的工作方式相同,但实现方式不同。

可打包界面:

如果你选择 Parcelable 接口,你的ItemDetails类必须实现 Parcelable。然后你可以把它作为

intent.putParcelableArrayListExtra("key", value);

并将其检索为:

getIntent().getParcelableArrayListExtra("key");

我不会为您编写 Parcelable 实现,因为它需要更多代码。这是一个很好的例子

可序列化接口:

如果您选择 Serializable 接口,我建议您创建名为 ItemDetailsWrapper 的类,它将包装您的ArrayList(s)<ItemDetails>

即 ItemDetailsWrapper 和 ItemDetails 类都必须实现 Serializable 接口。现在您可以像这样通过 Intent 传递它:

getIntent().putExtra("key", <serializableClass>); // storing 
getIntent().getSerializableExtra("key"); // retrieving

实施示例:

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
}

以及如何通过活动:

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();
于 2013-03-31T15:38:19.350 回答