0

I'm trying to pass one object from my MainActivity to another. It has an atribute of the type ArrayList which is changed and I don't know why. In the MainActivity I use object's toString and System.out.println() to see what he has and I can see this:

Incidente{name='Farola fundida', type='sin luz', street='falsa', streetNumber=123, city='Bilbao', contacts=[alguien1, alguien2]}

However, when I receive it on the other activity I make another System.out.println() and I get this:

Incidente{name='Farola fundida', type='sin luz', street='falsa', streetNumber=123, city='Bilbao', contacts=[, null]}

Can you tell me what I'm doing wrong?

My class Incidente code is this:

public class Incidente implements Parcelable{

    String name;
    String type;
    String street;
    int streetNumber;
    String city;
    ArrayList<String> contacts;

    public Incidente(String name, String type, String street, int streetNumber, String city, ArrayList<String> contacts) {
        this.name = name;
        this.type = type;
        this.street = street;
        this.streetNumber = streetNumber;
        this.city = city;
        this.contacts = contacts;
    }


    protected Incidente(Parcel in) {
        name = in.readString();
        type = in.readString();
        street = in.readString();
        streetNumber = in.readInt();
        city = in.readString();
        contacts = in.createStringArrayList();
    }

    public static final Creator<Incidente> CREATOR = new Creator<Incidente>() {
        @Override
        public Incidente createFromParcel(Parcel in) {
            return new Incidente(in);
        }

        @Override
        public Incidente[] newArray(int size) {
            return new Incidente[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(type);
        dest.writeString(street);
        dest.writeInt(streetNumber);
        dest.writeString(city);
        dest.writeList(contacts);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public int getStreetNumber() {
        return streetNumber;
    }

    public void setStreetNumber(int streetNumber) {
        this.streetNumber = streetNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public ArrayList<String> getContacts() {
        return contacts;
    }

    public void setContacts(ArrayList<String> contacts) {
        this.contacts = contacts;
    }

    @Override
    public String toString() {
        return "Incidente{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", street='" + street + '\'' +
                ", streetNumber=" + streetNumber +
                ", city='" + city + '\'' +
                ", contacts=" + contacts +
                '}';
    }

    public boolean equals (Incidente obj2)
    {

        if(this.getName().equals(obj2.getName()))
        {
            if(this.getType().equals(obj2.getType()))
            {
                if(this.getStreet().equals(obj2.getStreet()))
                {
                    if(this.getStreetNumber()== obj2.getStreetNumber())
                    {
                        if(this.getCity().equals(obj2.getStreetNumber()))
                        {
                            if(this.getContacts().equals(obj2.getContacts()))
                            {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
}

This is how I send the object:

ArrayList<String> arrContactos=new ArrayList<String>();
arrContactos.add("alguien1");
arrContactos.add("alguien2");
Incidente inci = new Incidente("Farola fundida", "sin luz", "falsa", 123, "Bilbao", arrContactos);

Intent i = new Intent(this, DetalleIncidenteActivity.class);
i.putExtra("objeto",inci);

And this is how I receive it:

incidente=getIntent().getExtras().getParcelable("objeto");
Euskalduna
  • 1,089
  • 1
  • 9
  • 11
  • Not related to the error. But read this for overriding equals..http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – Renjith Nov 04 '15 at 09:23

2 Answers2

3

From the documentation of createStringArrayList:

Read and return a new ArrayList containing String objects from the parcel that was written with writeStringList(List) at the current dataPosition().

So, accordingly to the documentation you should be using writeStringList(List) instead of writeList

Community
  • 1
  • 1
Blackbelt
  • 148,780
  • 26
  • 271
  • 285
0

Generated using parcelabler

public class Incidente implements Parcelable, Parcelable {

        String name;
        String type;
        String street;
        int streetNumber;
        String city;
        ArrayList<String> contacts;

        protected Incidente(Parcel in) {
            name = in.readString();
            type = in.readString();
            street = in.readString();
            streetNumber = in.readInt();
            city = in.readString();
            if (in.readByte() == 0x01) {
                contacts = new ArrayList<String>();
                in.readList(contacts, String.class.getClassLoader());
            } else {
                contacts = null;
            }
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeString(type);
            dest.writeString(street);
            dest.writeInt(streetNumber);
            dest.writeString(city);
            if (contacts == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeList(contacts);
            }
        }

        @SuppressWarnings("unused")
        public static final Parcelable.Creator<Incidente> CREATOR = new Parcelable.Creator<Incidente>() {
            @Override
            public Incidente createFromParcel(Parcel in) {
                return new Incidente(in);
            }

            @Override
            public Incidente[] newArray(int size) {
                return new Incidente[size];
            }
        };
    }
Renjith
  • 3,130
  • 16
  • 39