0

I want to pass ArrayList via Intent to another activity. However, the code doesn't give any errors but List is always empty. Any idea what i'm doing wrong ? ty

Activity1

 private ArrayList<ResimBean> rbList = new ArrayList<ResimBean>();



                Bitmap bmp = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] thumbArray = stream.toByteArray();
                 Uri selectedImageUri = data.getData();
                String fotopath = getRealPathFromURI(selectedImageUri);
                ResimBean rb = new ResimBean(Parcel.obtain());
               // rb.setResim(bar);
                rb.setThumbnail(thumbArray);
                rb.setPath(fotopath);
                rbList.add(rb);
     Intent intent = new Intent(getApplicationContext(), ResimListActivity.class);
     intent.putParcelableArrayListExtra("reslist",rbList);
     startActivityForResult(intent, 100);

Activity2

Bundle extras = getIntent().getExtras();
if (extras != null) {
    try {
       Intent i = getIntent();
       ArrayList<ResimBean> rbList = i.getParcelableArrayListExtra("reslist");
    } catch (Exception ex) {
      String msg = ex.getMessage(); 
    }
}

Its not giving any error but list is always empty.

EDIT

Added the code how i fill in list.

EDIT 2 Something wrong with my Parcelable class or what ?

 public class ResimBean implements Parcelable {
 private int Id;
 private int HataBildirimId;
 private byte[] Resim;
 private byte[] Thumbnail;
 public byte[] getThumbnail() {
    return Thumbnail;
}

public void setThumbnail(byte[] thumbnail) {
    Thumbnail = thumbnail;
}

private String Path;

public String getPath() {
    return Path;
}

public void setPath(String path) {
    Path = path;
}

public int getHataBildirimId() {
    return HataBildirimId;
}

public void setHataBildirimId(int hataBildirimId) {
    HataBildirimId = hataBildirimId;
}

public int getId() {
    return Id;
}

public void setId(int id) {
    Id = id;
}

public byte[] getResim() {
    return Resim;
}

public void setResim(byte[] resim) {
    Resim = resim;
}
@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(HataBildirimId);
    dest.writeByteArray(Resim);
    dest.writeByteArray(Thumbnail);

}
public ResimBean(Parcel in) {
    readFromParcel(in);
}

public void readFromParcel(Parcel in){
    this.HataBildirimId = in.readInt();
    this.Resim = new byte[in.readInt()];
    this.Thumbnail = new byte[in.readInt()];
}

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

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

2 Answers2

0

You can follow this tutorial: http://aryo.lecture.ub.ac.id/android-passing-arraylist-of-object-within-an-intent/

I got it working like this

Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            try {
                Intent i = getIntent();
                ArrayList<ResimBean> rbList = (ArrayList<ResimBean>) i.getExtras().get("reslist");
                Log.i("mytag", " "+i.getExtras().get("reslist").toString());
                Log.i("mytag", " "+rbList.get(0).toString());
            } catch (Exception ex) {
                String msg = ex.getMessage();
            }
        }

With the rbList in Activity2 size=1, With your code I was getting size=0

Isaac Urbina
  • 1,295
  • 11
  • 21
  • List isnt empty i just added the code how i fill in it. Check my updated code. Sry about it –  Apr 12 '16 at 15:05
0

The way you are showing, you create a new ArrayList<> and send it empty as extra via intent to the next activity.

Where exactly do you populate your ArrayList?

You should do something like this:

private ArrayList<ResimBean> rbList = new ArrayList<ResimBean>();

//populate rbList using adapter, then call intent

Intent intent = new Intent(getApplicationContext(), ResimListActivity.class);
intent.putParcelableArrayListExtra("reslist",rbList);
startActivityForResult(intent, 100);

Otherwise, the rbList you send as extra will always be empty. It sounds obvious but I don't know how you are doing it, so this is my best guess.

  • Take a look at this answer: [here](http://stackoverflow.com/questions/1626667/how-to-use-parcel-in-android). It may help you. I don't think you should create a new Object using Parcel.obtain() as parameter. here: – Felipe Kelemen Apr 12 '16 at 18:42