amr el malah
amr el malah

Reputation: 31

android pass object contains bitMap to another activity

i am new in android .and i need some help i have ArrayList which is an object implements Parciable .and i want to pass this ArrayList to another Activity. this is my NewsEntity.java

public class NewsEntity implements Parcelable{ /** * */ private Bitmap bitmap; private String title; private String brief; private String details; private String reporter; private String ImageFile; private String date; private String readCount; private String shareCount; public int addRead_count; public int addShare_count;

private static ByteBuffer dst;
private static byte[] bytesar;
public NewsEntity() {
    bitmap=null;
    title="";
    brief="";
    details="";
    reporter="";
    ImageFile="";
    date="";
    readCount="";
    shareCount="";
    addRead_count=0;
    addShare_count=0;
}

public void setTitle(String title) {
    this.title = title;
}
public String getTitle() {
    return title;
}
public void setBrief(String brief) {
    this.brief = brief;
}
public String getBrief() {
    return brief;
}
public void setDetails(String details) {
    this.details = details;
}
public String getDetails() {
    return details;
}
public void setReporter(String reporter) {
    this.reporter = reporter;
}
public String getReporter() {
    return reporter;
}

public void setImageFile(String imageFile) {
    ImageFile = imageFile;
}
public String getImageFile() {
    return ImageFile;
}
public void setDate(String date) {
    this.date = date;
}
public String getDate() {
    return date;
}
public void setReadCount(String readCount) {
    this.readCount = readCount;
}
public String getReadCount() {
    return readCount;
}
public void setShareCount(String shareCount) {
    this.shareCount = shareCount;
}
public String getShareCount() {
    return shareCount;
}

public Bitmap getBitmap() {
        return bitmap;
}

public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
}

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

    @Override
    public void writeToParcel(Parcel out, int flags) {
         try{
             out.writeString(title);
         out.writeString(brief);
         out.writeString(details);
         out.writeString(reporter);
         out.writeString(readCount);
         out.writeString(shareCount);
         out.writeInt(addRead_count);
         out.writeInt(addShare_count);
         out.writeString(date);
         out.writeString(ImageFile);

    }

     public static final Parcelable.Creator<NewsEntity> CREATOR = new Parcelable.Creator<NewsEntity>() {
         //public class MyCreator implements Parcelable.Creator<MyProduct> {    
          public NewsEntity createFromParcel(Parcel in) {
                    return new NewsEntity();
                }

                public NewsEntity[] newArray(int size) {
                    return new NewsEntity[size];
                }
            };
            private NewsEntity(Parcel in) {
                try{
                    title=(String) in.readString();
                 brief=(String) in.readString();
                 details=(String) in.readString();
                 date=(String) in.readString();
                 reporter=(String) in.readString();
                 readCount=(String) in.readString();
                 shareCount=(String) in.readString();
                 ImageFile=(String) in.readString();
                 addRead_count=(Integer) in.readInt();
                 addShare_count=(Integer) in.readInt();
                bitmap=in.readParcelable(Bitmap.class.getClassLoader());

                }
                catch(Exception e){}
               }

}

and this line which to pass ArrayList with intent newsDetailsIntent.putExtra("display", result_news); startActivity(newsDetailsIntent);

i need help.how to do it

thanks in advance

Upvotes: 2

Views: 1550

Answers (1)

Thiengo
Thiengo

Reputation: 294

Use your class like this (Bitmap):

package br.problema.domain;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

public class Product implements Parcelable {
    private long id;
    private String name;
    private String tag;
    private Bitmap image;


    public Product(long id, String name, String tag, Bitmap image) {
        super();
        this.id = id;
        this.name = name;
        this.tag = tag;
        this.image = image;
    }
    public Product(Parcel in){
        id = in.readLong();
        name = in.readString();
        tag = in.readString();
        image = (Bitmap) in.readValue(Bitmap.class.getClassLoader());
    }


    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }


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


    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
    }


    public Bitmap getImage() {
        return image;
    }
    public void setImage(Bitmap image) {
        this.image = image;
    }


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


    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(name);
        dest.writeString(tag);
        dest.writeValue(image);
    }


    public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>(){
         public Product createFromParcel(Parcel in){
             return new Product(in);
         }

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

Upvotes: 2

Related Questions