0

I have a private List<Card> characters; which contains Card objects. I need to pass it through an intent.

How do I do it? Tried putParcelableArrayListExtra() but can't retrieve it or maybe I'm doing it in the wrong way, any ideas?

This is what I tried:

Intent intent = new Intent(this, RandomAssignment.class);
intent.putParcelableArrayListExtra("CHARACTERS", (ArrayList<? extends Parcelable>) characters);
startActivity(intent);

And to retrieve it:

characters = this.getIntent().getParcelableArrayListExtra("CHARACTERS");

EDIT:

I've implemented those 2 methods in the Card Class, but there are some other classes which extend Card. And I get this error on them:

Error:(7, 25) error: constructor Card in class Card cannot be applied to given types; required: Parcel found: no arguments reason: actual and formal argument lists differ in length

This is the code of one of the other classes which extend Card (they're very similar from one another):

public class Clairvoyant extends Card {

public Clairvoyant(){
    this.setName("Clairvoyant");
}

public static boolean point(Card d){
    return d.getClass() == Wolf.class;
}
}
FET
  • 942
  • 4
  • 13
  • 35
  • Also show parcelable implementation – Tim Jun 02 '16 at 15:15
  • Posted all the code guys @Shriram – FET Jun 02 '16 at 15:18
  • Check now @TimCastelijns – FET Jun 02 '16 at 15:18
  • 2
    This is not all the code. How do you encode/decode your class into a parcelable object? – Tim Jun 02 '16 at 15:20
  • Ehm, I didn't probably, that's why I'm asking your help in order to fix this, how should I do it? Generally, how would you pass such a list through an intent? @TimCastelijns – FET Jun 02 '16 at 15:23
  • Your Card object needs to implement the `Parcelable` interface. [this question might help you](http://stackoverflow.com/questions/7181526/how-can-i-make-my-custom-objects-be-parcelable) – Carlos J Jun 02 '16 at 15:34
  • Look at this: http://stackoverflow.com/a/23333613/4350275 – Prerak Sola Jun 02 '16 at 15:36
  • @PrerakSola When I implements the Parcelable interface Android Studio tells me I've either to set it as an abstract class or create 2 methods: `describeContents()` and `writeToParcel()`. What should I do? – FET Jun 02 '16 at 17:27
  • I tried to sei as abstract and it was looking good, until I ran it: those objects (characters) who extends Card (set as abstract now) need to be abstract, but if I set 'em abstract I can't instantiate them! Help, I'm stuck @PrerakSola – FET Jun 02 '16 at 17:32
  • @SteveKuo I don't really understand how should I send this using parcelable, may I ask you a brief explanation? – FET Jun 02 '16 at 18:08
  • Don't make it abstract. Create those two methods. Those methods are there in the link I provided. Have a look at it and try to replicate as per your class. – Prerak Sola Jun 02 '16 at 18:51
  • Alright, but I don't understand what the second one should do, or at least how it should be applied to my code @PrerakSola – FET Jun 02 '16 at 19:15
  • @PrerakSola Done! Now I got another error, can you find a way? – FET Jun 03 '16 at 19:22
  • @CarlosJ Got a new error, see the updated question to see that, any ideas? – FET Jun 03 '16 at 19:23

0 Answers0