Passing a List to another Activity in Android

后端 未结 3 529
生来不讨喜
生来不讨喜 2020-12-01 16:47

I\'ve created a list and would like to pass the list to another activity but i\'m getting an error on the putExtra statement when i create the intent. Just wondering is ther

3条回答
  •  失恋的感觉
    2020-12-01 17:49

    I know it is a little late and this question has an answer already but here is another way.

    simply create another object define it as Serializable and give it a list variable and send that using putExtra on your intent like this:

    public class Category implements Serializable {
    private List objects;
    
    public Category() {
    }
    
    public List getObjects() {
        return objects;
    }
    
    public void setObjects(List objects) {
        this.objects = objects;
    }
    

    and then for sending it do this:

    Category cat = new Category();
    cat.setObjects(objects);
    intent.putExtra("listOfObjects",cat);
    startActivity(intent);
    

    and to get the object you have created do this:

    Category cat = (Category) extras.get("listOfObjects");
    cat.getObjects;
    

提交回复
热议问题