Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass ArrayList<? implements Parcelable> to Activity

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

public class Series implements Parcelable { private String name; private int numOfSeason; private int numOfEpisode;  /** Constructors and Getters/Setters have been removed to make reading easier **/  public Series(Parcel in) {     String[] data = new String[3];     in.readStringArray(data);     this.name = data[0];     this.numOfSeason = Integer.parseInt(data[1]);     this.numOfEpisode = Integer.parseInt(data[2]); }   @Override public int describeContents() {     return 0; }  @Override public void writeToParcel(Parcel dest, int flags) {     dest.writeStringArray(new String[] { this.name,             String.valueOf(this.numOfSeason),             String.valueOf(this.numOfEpisode) });  }  private void readFromParcel(Parcel in) {     name = in.readString();     numOfSeason = in.readInt();     numOfEpisode = in.readInt(); }  public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {     @Override     public Series createFromParcel(Parcel in) {         return new Series(in);     }      @Override     public Series[] newArray(int size) {         return new Series[size];     } }; 

}

In my MainActivity I have an ArrayList. To make the list dynamically editeable I need to pass it to another activity where I can edit it.

ArrayList<Series> listOfSeries = new ArrayList<Series>();      public void openAddActivity() {     Intent intent = new Intent(this, AddActivity.class);     intent.putParcelableArrayListExtra(             "com.example.episodetracker.listofseries",             (ArrayList<? extends Parcelable>) listOfSeries);     startActivity(intent); } 

I need to cast the list, otherwise Eclipse gives me the following Error message. The method putParcelableArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, List)

Is this the correct way to do it?

    ArrayList<Series> list = savedInstanceState             .getParcelableArrayList("com.example.episodetracker.listofseries"); 

This is the way I try to read the data in another activity.

It's crashing on the line above. namely the getParcelableArrayList part.

like image 872
Adrian Jandl Avatar asked Feb 28 '13 10:02

Adrian Jandl


People also ask

How do you implement Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

How pass arrayList from activity to fragment in Android?

Intent intent = new Intent(getActivity(), StudentResult. class); intent. putExtra("ExtraData", allStudents); startActivity(intent); and in target class to show the objects in ListView();

How do I use Parcelable intent?

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity: Intent intent = new Intent(getBaseContext(), NextActivity. class); Foo foo = new Foo(); intent. putExtra("foo ", foo); startActivity(intent);


2 Answers

  • The problem is in writing out to the parcel and reading in from the parcel ...

    @Override public void writeToParcel(Parcel dest, int flags) {     dest.writeString(name);     dest.writeInt(numOfSeason);     dest.writeInt(numOfEpisode); }  private void readFromParcel(Parcel in) {     name = in.readString();     numOfSeason = in.readInt();     numOfEpisode = in.readInt(); } 
  • What you write out has to match what you read in...

    @Override  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);  Intent i = new Intent(this,SecondActivity.class);  ArrayList<testparcel> testing = new ArrayList<testparcel>();  i.putParcelableArrayListExtra("extraextra", testing); startActivity(i); }      /**********************************************/   public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      ArrayList<testparcel> testing = this.getIntent().getParcelableArrayListExtra("extraextra");  } } 
  • The above code is having onCreate() from two different activities. The first one launches the second one; and it works fine I was able to pull the parcelable without issue.

like image 83
snowCrabs Avatar answered Oct 01 '22 12:10

snowCrabs


You should use the putParcelableArrayListExtra() method on the Intent class.

like image 39
Ovidiu Latcu Avatar answered Oct 01 '22 11:10

Ovidiu Latcu