android extra list,android - Array List Intent extra in Java - Stack Overflow

But i cannot find how am ı supposed to write to get arrayList

Short answer: you can't. You can only pass ArrayList and then retrieve it with

getIntent().getStringArrayListExtra("key");

But. If you want to pass custom objects via Intent your objects have to implement:

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

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.

Serializable interface:

If you choose Serializable interface i suggest you to create class named for instance ItemDetailsWrapper that will wrap your ArrayList(s)

Both i.e. ItemDetailsWrapper and ItemDetails class have to implement Serializable interface. Now you are able to pass it via Intent like this:

getIntent().putExtra("key", ); // storing

getIntent().getSerializableExtra("key"); // retrieving

Example of implementation:

public class ItemDetailsWrapper implements Serializable {

private static final long serialVersionUID = 1L;

private ArrayList itemDetails;

public ItemDetailsWrapper(ArrayList items) {

this.itemDetails = items;

}

public ArrayList 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(, );

i.putExtra("obj", wrapper); // i.putExtra("obj", new ItemDetailsWrapper(list));

// retrieving

ItemDetailsWrapper wrap =

(ItemDetailsWrapper) getIntent().getSerializableExtra("obj");

ArrayList list = wrap.getItemDetails();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值