Pass ArrayList to Activity












29















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.










share|improve this question

























  • 1/ i don't understand what doesn't work in your code. 2/ don't you save the lists somewhere ? therefore, wouldn't it be best to work on the database rather than on an array you pass from activity to activity ?

    – njzk2
    Feb 28 '13 at 10:38











  • Parcelables are the devil so to speak... have you tried making it Serializable instead?

    – snowCrabs
    Feb 28 '13 at 14:27






  • 1





    I was under the impression that Parcelable is the way to go since Serializable is a lot slower ?

    – Adrian Jandl
    Feb 28 '13 at 14:31











  • Parcelable makes it easier for complex classes. As far as speed goes how many data entries are we talking about?

    – snowCrabs
    Feb 28 '13 at 14:54











  • An ArrayList with ~10 entries, where each entry has 3 memvars.

    – Adrian Jandl
    Feb 28 '13 at 14:57
















29















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.










share|improve this question

























  • 1/ i don't understand what doesn't work in your code. 2/ don't you save the lists somewhere ? therefore, wouldn't it be best to work on the database rather than on an array you pass from activity to activity ?

    – njzk2
    Feb 28 '13 at 10:38











  • Parcelables are the devil so to speak... have you tried making it Serializable instead?

    – snowCrabs
    Feb 28 '13 at 14:27






  • 1





    I was under the impression that Parcelable is the way to go since Serializable is a lot slower ?

    – Adrian Jandl
    Feb 28 '13 at 14:31











  • Parcelable makes it easier for complex classes. As far as speed goes how many data entries are we talking about?

    – snowCrabs
    Feb 28 '13 at 14:54











  • An ArrayList with ~10 entries, where each entry has 3 memvars.

    – Adrian Jandl
    Feb 28 '13 at 14:57














29












29








29


11






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.










share|improve this question
















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.







android arraylist parcelable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 '13 at 15:02







Adrian Jandl

















asked Feb 28 '13 at 10:28









Adrian JandlAdrian Jandl

2,20441625




2,20441625













  • 1/ i don't understand what doesn't work in your code. 2/ don't you save the lists somewhere ? therefore, wouldn't it be best to work on the database rather than on an array you pass from activity to activity ?

    – njzk2
    Feb 28 '13 at 10:38











  • Parcelables are the devil so to speak... have you tried making it Serializable instead?

    – snowCrabs
    Feb 28 '13 at 14:27






  • 1





    I was under the impression that Parcelable is the way to go since Serializable is a lot slower ?

    – Adrian Jandl
    Feb 28 '13 at 14:31











  • Parcelable makes it easier for complex classes. As far as speed goes how many data entries are we talking about?

    – snowCrabs
    Feb 28 '13 at 14:54











  • An ArrayList with ~10 entries, where each entry has 3 memvars.

    – Adrian Jandl
    Feb 28 '13 at 14:57



















  • 1/ i don't understand what doesn't work in your code. 2/ don't you save the lists somewhere ? therefore, wouldn't it be best to work on the database rather than on an array you pass from activity to activity ?

    – njzk2
    Feb 28 '13 at 10:38











  • Parcelables are the devil so to speak... have you tried making it Serializable instead?

    – snowCrabs
    Feb 28 '13 at 14:27






  • 1





    I was under the impression that Parcelable is the way to go since Serializable is a lot slower ?

    – Adrian Jandl
    Feb 28 '13 at 14:31











  • Parcelable makes it easier for complex classes. As far as speed goes how many data entries are we talking about?

    – snowCrabs
    Feb 28 '13 at 14:54











  • An ArrayList with ~10 entries, where each entry has 3 memvars.

    – Adrian Jandl
    Feb 28 '13 at 14:57

















1/ i don't understand what doesn't work in your code. 2/ don't you save the lists somewhere ? therefore, wouldn't it be best to work on the database rather than on an array you pass from activity to activity ?

– njzk2
Feb 28 '13 at 10:38





1/ i don't understand what doesn't work in your code. 2/ don't you save the lists somewhere ? therefore, wouldn't it be best to work on the database rather than on an array you pass from activity to activity ?

– njzk2
Feb 28 '13 at 10:38













Parcelables are the devil so to speak... have you tried making it Serializable instead?

– snowCrabs
Feb 28 '13 at 14:27





Parcelables are the devil so to speak... have you tried making it Serializable instead?

– snowCrabs
Feb 28 '13 at 14:27




1




1





I was under the impression that Parcelable is the way to go since Serializable is a lot slower ?

– Adrian Jandl
Feb 28 '13 at 14:31





I was under the impression that Parcelable is the way to go since Serializable is a lot slower ?

– Adrian Jandl
Feb 28 '13 at 14:31













Parcelable makes it easier for complex classes. As far as speed goes how many data entries are we talking about?

– snowCrabs
Feb 28 '13 at 14:54





Parcelable makes it easier for complex classes. As far as speed goes how many data entries are we talking about?

– snowCrabs
Feb 28 '13 at 14:54













An ArrayList with ~10 entries, where each entry has 3 memvars.

– Adrian Jandl
Feb 28 '13 at 14:57





An ArrayList with ~10 entries, where each entry has 3 memvars.

– Adrian Jandl
Feb 28 '13 at 14:57












5 Answers
5






active

oldest

votes


















37
















  • 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.







share|improve this answer


























  • Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

    – Adrian Jandl
    Feb 28 '13 at 14:50













  • The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

    – snowCrabs
    Feb 28 '13 at 14:59











  • Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

    – Adrian Jandl
    Feb 28 '13 at 15:03








  • 1





    You have to call from the the intent not the savedInstanceState

    – snowCrabs
    Feb 28 '13 at 15:12











  • I went round in circles for an hour... everything was perfect except the order...

    – me_
    Jul 27 '18 at 23:28



















3














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






share|improve this answer
























  • intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

    – Adrian Jandl
    Feb 28 '13 at 11:35






  • 11





    declare your listOfSeries as ArrayList not List.

    – Ovidiu Latcu
    Feb 28 '13 at 13:38






  • 1





    Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

    – Adrian Jandl
    Feb 28 '13 at 14:19





















3














I've used putParcelableArrayList(<? extends Parcelable>) 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<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
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<ParcelableRow> 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.






share|improve this answer































    0














    Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth






    share|improve this answer































      0














      I am doing it in this way:



      var intent = Intent(this@McqActivity,ResultActivity::class.java)
      intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
      // resultList is of type mutableListOf<ResultBO>()
      startActivity(intent)


      And for making the class Parcelable . I simply do two operation first I used @Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..



      import kotlinx.android.parcel.Parcelize
      import android.os.Parcelable

      @Parcelize // Include Annotion
      data class ResultBO(val questionBO: QuestionBO) : Parcelable {
      constructor() : this(QuestionBO())
      }


      And at receiving end



      if (intent != null) {
      var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
      }





      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15133121%2fpass-arraylist-implements-parcelable-to-activity%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        37
















        • 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.







        share|improve this answer


























        • Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 14:50













        • The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

          – snowCrabs
          Feb 28 '13 at 14:59











        • Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 15:03








        • 1





          You have to call from the the intent not the savedInstanceState

          – snowCrabs
          Feb 28 '13 at 15:12











        • I went round in circles for an hour... everything was perfect except the order...

          – me_
          Jul 27 '18 at 23:28
















        37
















        • 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.







        share|improve this answer


























        • Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 14:50













        • The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

          – snowCrabs
          Feb 28 '13 at 14:59











        • Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 15:03








        • 1





          You have to call from the the intent not the savedInstanceState

          – snowCrabs
          Feb 28 '13 at 15:12











        • I went round in circles for an hour... everything was perfect except the order...

          – me_
          Jul 27 '18 at 23:28














        37












        37








        37









        • 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.







        share|improve this answer

















        • 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.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '17 at 14:11









        NarendraJi

        3,01832470




        3,01832470










        answered Feb 28 '13 at 14:36









        snowCrabssnowCrabs

        70548




        70548













        • Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 14:50













        • The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

          – snowCrabs
          Feb 28 '13 at 14:59











        • Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 15:03








        • 1





          You have to call from the the intent not the savedInstanceState

          – snowCrabs
          Feb 28 '13 at 15:12











        • I went round in circles for an hour... everything was perfect except the order...

          – me_
          Jul 27 '18 at 23:28



















        • Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 14:50













        • The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

          – snowCrabs
          Feb 28 '13 at 14:59











        • Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

          – Adrian Jandl
          Feb 28 '13 at 15:03








        • 1





          You have to call from the the intent not the savedInstanceState

          – snowCrabs
          Feb 28 '13 at 15:12











        • I went round in circles for an hour... everything was perfect except the order...

          – me_
          Jul 27 '18 at 23:28

















        Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

        – Adrian Jandl
        Feb 28 '13 at 14:50







        Changed it. However the NullPointerException remains. Haven't changed anything in this part though ` ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

        – Adrian Jandl
        Feb 28 '13 at 14:50















        The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

        – snowCrabs
        Feb 28 '13 at 14:59





        The name of it in the bundle doesn't really matter you could call it "MyArray" as the key and it should work. What line is it crashing on?

        – snowCrabs
        Feb 28 '13 at 14:59













        Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

        – Adrian Jandl
        Feb 28 '13 at 15:03







        Crashing on the line where I "load" in my new activity. Specifically this line: ArrayList<Series> list = savedInstanceState .getParcelableArrayList("com.example.episodetracker.listofseries");`

        – Adrian Jandl
        Feb 28 '13 at 15:03






        1




        1





        You have to call from the the intent not the savedInstanceState

        – snowCrabs
        Feb 28 '13 at 15:12





        You have to call from the the intent not the savedInstanceState

        – snowCrabs
        Feb 28 '13 at 15:12













        I went round in circles for an hour... everything was perfect except the order...

        – me_
        Jul 27 '18 at 23:28





        I went round in circles for an hour... everything was perfect except the order...

        – me_
        Jul 27 '18 at 23:28













        3














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






        share|improve this answer
























        • intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

          – Adrian Jandl
          Feb 28 '13 at 11:35






        • 11





          declare your listOfSeries as ArrayList not List.

          – Ovidiu Latcu
          Feb 28 '13 at 13:38






        • 1





          Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

          – Adrian Jandl
          Feb 28 '13 at 14:19


















        3














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






        share|improve this answer
























        • intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

          – Adrian Jandl
          Feb 28 '13 at 11:35






        • 11





          declare your listOfSeries as ArrayList not List.

          – Ovidiu Latcu
          Feb 28 '13 at 13:38






        • 1





          Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

          – Adrian Jandl
          Feb 28 '13 at 14:19
















        3












        3








        3







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






        share|improve this answer













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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 28 '13 at 10:33









        Ovidiu LatcuOvidiu Latcu

        47.1k136781




        47.1k136781













        • intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

          – Adrian Jandl
          Feb 28 '13 at 11:35






        • 11





          declare your listOfSeries as ArrayList not List.

          – Ovidiu Latcu
          Feb 28 '13 at 13:38






        • 1





          Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

          – Adrian Jandl
          Feb 28 '13 at 14:19





















        • intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

          – Adrian Jandl
          Feb 28 '13 at 11:35






        • 11





          declare your listOfSeries as ArrayList not List.

          – Ovidiu Latcu
          Feb 28 '13 at 13:38






        • 1





          Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

          – Adrian Jandl
          Feb 28 '13 at 14:19



















        intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

        – Adrian Jandl
        Feb 28 '13 at 11:35





        intent.putParcelableArrayListExtra( "com.example.episodetracker.listofseries", (ArrayList<? extends Parcelable>) listOfSeries); is this the correct way to do it? If I don't cast it, I get the following error: The method putParcelableArrayListExtra(String, ArrayList<? extends Parcelable>) in the type Intent is not applicable for the arguments (String, List<Series>)

        – Adrian Jandl
        Feb 28 '13 at 11:35




        11




        11





        declare your listOfSeries as ArrayList not List.

        – Ovidiu Latcu
        Feb 28 '13 at 13:38





        declare your listOfSeries as ArrayList not List.

        – Ovidiu Latcu
        Feb 28 '13 at 13:38




        1




        1





        Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

        – Adrian Jandl
        Feb 28 '13 at 14:19







        Alright, that made the compiler happy. However, the program still crashes once I try to read the data. I've updated the original post with a logcat snippet.

        – Adrian Jandl
        Feb 28 '13 at 14:19













        3














        I've used putParcelableArrayList(<? extends Parcelable>) 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<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
        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<ParcelableRow> 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.






        share|improve this answer




























          3














          I've used putParcelableArrayList(<? extends Parcelable>) 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<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
          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<ParcelableRow> 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.






          share|improve this answer


























            3












            3








            3







            I've used putParcelableArrayList(<? extends Parcelable>) 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<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
            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<ParcelableRow> 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.






            share|improve this answer













            I've used putParcelableArrayList(<? extends Parcelable>) 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<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
            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<ParcelableRow> 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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 14 '13 at 22:58









            AXSMAXSM

            6091725




            6091725























                0














                Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth






                share|improve this answer




























                  0














                  Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth






                  share|improve this answer


























                    0












                    0








                    0







                    Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth






                    share|improve this answer













                    Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 25 '15 at 13:46









                    Peter IsbergPeter Isberg

                    1,88221628




                    1,88221628























                        0














                        I am doing it in this way:



                        var intent = Intent(this@McqActivity,ResultActivity::class.java)
                        intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
                        // resultList is of type mutableListOf<ResultBO>()
                        startActivity(intent)


                        And for making the class Parcelable . I simply do two operation first I used @Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..



                        import kotlinx.android.parcel.Parcelize
                        import android.os.Parcelable

                        @Parcelize // Include Annotion
                        data class ResultBO(val questionBO: QuestionBO) : Parcelable {
                        constructor() : this(QuestionBO())
                        }


                        And at receiving end



                        if (intent != null) {
                        var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
                        }





                        share|improve this answer






























                          0














                          I am doing it in this way:



                          var intent = Intent(this@McqActivity,ResultActivity::class.java)
                          intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
                          // resultList is of type mutableListOf<ResultBO>()
                          startActivity(intent)


                          And for making the class Parcelable . I simply do two operation first I used @Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..



                          import kotlinx.android.parcel.Parcelize
                          import android.os.Parcelable

                          @Parcelize // Include Annotion
                          data class ResultBO(val questionBO: QuestionBO) : Parcelable {
                          constructor() : this(QuestionBO())
                          }


                          And at receiving end



                          if (intent != null) {
                          var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
                          }





                          share|improve this answer




























                            0












                            0








                            0







                            I am doing it in this way:



                            var intent = Intent(this@McqActivity,ResultActivity::class.java)
                            intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
                            // resultList is of type mutableListOf<ResultBO>()
                            startActivity(intent)


                            And for making the class Parcelable . I simply do two operation first I used @Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..



                            import kotlinx.android.parcel.Parcelize
                            import android.os.Parcelable

                            @Parcelize // Include Annotion
                            data class ResultBO(val questionBO: QuestionBO) : Parcelable {
                            constructor() : this(QuestionBO())
                            }


                            And at receiving end



                            if (intent != null) {
                            var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
                            }





                            share|improve this answer















                            I am doing it in this way:



                            var intent = Intent(this@McqActivity,ResultActivity::class.java)
                            intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
                            // resultList is of type mutableListOf<ResultBO>()
                            startActivity(intent)


                            And for making the class Parcelable . I simply do two operation first I used @Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..



                            import kotlinx.android.parcel.Parcelize
                            import android.os.Parcelable

                            @Parcelize // Include Annotion
                            data class ResultBO(val questionBO: QuestionBO) : Parcelable {
                            constructor() : this(QuestionBO())
                            }


                            And at receiving end



                            if (intent != null) {
                            var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '18 at 12:02









                            Moritz

                            57.7k19132184




                            57.7k19132184










                            answered Nov 13 '18 at 11:51









                            Xar E AhmerXar E Ahmer

                            22.3k8150206




                            22.3k8150206






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15133121%2fpass-arraylist-implements-parcelable-to-activity%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Full-time equivalent

                                さくらももこ

                                13 indicted, 8 arrested in Calif. drug cartel investigation