Pass ArrayList<? implements Parcelable> to Activity

前端 未结 6 1828
礼貌的吻别
礼貌的吻别 2020-11-27 04:20

I have searched a few topics but not found a solution to my problem.

public class Series implements Parcelable {
private String name;
private int numOfSeaso         


        
6条回答
  •  情深已故
    2020-11-27 04:52

    I've used putParcelableArrayList() from a Bundle Object. Not directly from an Intent Object.(I don't really know what's the difference). but i use to use in this way:

    ArrayList resultSet = new ArrayList();
    resultSet = loadData();
    
    Bundle data = new Bundle();
    data.putParcelableArrayList("search.resultSet", resultSet);
    yourIntent.putExtra("result.content", data);
    startActivity(yourIntent);
    

    Later on your new activity you can populate the data recently inserted on the Bundle object like this:

    Bundle data = this.getIntent().getBundleExtra("result.content");
    ArrayList result = data.getParcelableArrayList("search.resultset");
    

    Just remember that your ArrayList<> must contain only parcelable objects. and just to make sure that your have passed the data you may check if the data received is null or not, just to avoid issues.

提交回复
热议问题