Sending a custom object to an intentional android

I want to pass an object of type Annonce

to intent. As you can see, this is easy if the class attributes are primitives, however; in my case i have an image (bitmap) and a type attribute Client

(i created a class Client

). My solution is to access the attributes Client

(using getter and setter) and parsing in the method writeToParcel

(takes too long) and for the image I am posting it in mainActivity

using ByteArrayOutputStream

. Can anyone help me to do all this in class Annonce

.

public class Annonce implements Parcelable {

    String article, desc, temps, ville, categorie;
    int prix;
    Bitmap img;
    Client c;

    public Annonce(String article, String desc, String temps, String ville,
            String categorie, int prix, Bitmap img, Client c) {
        this.article = article;
        this.desc = desc;
        this.c = c;
        this.prix = prix;
        this.img = img;
        this.temps = temps;
        this.categorie = categorie;
        this.ville = ville;
    }

    public static final Parcelable.Creator<Annonce> CREATOR = new Parcelable.Creator<Annonce>() {

        public Annonce createFromParcel(Parcel source) {
            return new Annonce(source);
        }

        public Annonce[] newArray(int size) {
            return new Annonce[size];
        }
    };

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(article);
        parcel.writeString(desc);
        parcel.writeString(temps);
        parcel.writeString(ville);
        parcel.writeString(categorie);
        parcel.writeInt(prix);
    }

    public Annonce(Parcel source) {
        article = source.readString();
        desc = source.readString();
        temps = source.readString();
        ville = source.readString();
        categorie = source.readString();
        prix = source.readInt();
    }
}

      

+3


source to share


1 answer


Having a bitmap attribute is not a good solution. Instead, we can use the image path to refer to the bitmap. In addition, we can convert a Client to an object whenever possible, in order to send it using an intent.



0


source







All Articles