0

I will try to be as explanatory as possible, I am developing an Android app, in a Java class I have List with some data (objects), I want to transfer this list to another Java class (Activity), in order to use this list's data within this class.

here is my code:

public class DocumentariesCategoriesActivity extends Activity implements AdapterView.OnItemClickListener{

    private List<VideoEntity> videoSearchResult;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.d(TAG, "onItemClick(AdapterView<?> , View, int, long) - Ini");


        new Thread(new Runnable() {
            @Override
            public void run (){
                YouTubeConnector youtube  =  new YouTubeConnector(DocumentariesCategoriesActivity.this);
                videoSearchResult = youtube.search();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent =  new Intent(DocumentariesCategoriesActivity.this, DocumentariesCatalogActivity.class).putExtra("videos", (Parcelable) videoSearchResult);
                        startActivity(intent);
                    }
                });

            }
        }).start();
    }

As you can see in the code the activity I want to transfer the list to is DocumentariesCatalogActivity. I don't know if this is the correct way to pass it, if it is then my question will be how can I capture the VideoEntity List in the DocumentariesCatalogActivity class ?

I saw some similar questions but only with String type.

Sufian
  • 6,405
  • 16
  • 66
  • 120
rainman
  • 2,551
  • 6
  • 29
  • 43
  • 2
    duplicate of this question: http://stackoverflow.com/questions/12092612/pass-list-of-objects-from-one-activity-to-other-activity-in-android – Ahmed Ashraf Sep 03 '16 at 23:12
  • @AhmedAshrafG this did not resolve my problem, finally i resolved it, i put an answer – rainman Sep 04 '16 at 00:25
  • @rainman how did it not solve the problem? Aren't you using the same solution in your answer? – Sufian Sep 27 '16 at 10:28
  • @rainman I would like to know why you rolled back my edit which added relevant tags, improved title and code/post formatting. :) – Sufian Sep 27 '16 at 10:31

2 Answers2

2

I could resolve it implementing the Parcelable interface in my object class VideoEntity:

public class VideoEntity implements Parcelable{

private String title;
private String description;
private String thumbnailURL;
private String id;


public VideoEntity() {

}

public VideoEntity(Parcel in) {
    title = in.readString();
    description = in.readString();
    thumbnailURL = in.readString();
    id = in.readString();
}



public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getThumbnailURL() {
    return thumbnailURL;
}

public void setThumbnailURL(String thumbnail) {
    this.thumbnailURL = thumbnail;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(id);
    dest.writeString(title);
    dest.writeString(description);
    dest.writeString(thumbnailURL);
}

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

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

}

and in the Activity from where i want to transfer the list i changed this:

 Intent intent =  new Intent(DocumentariesCategoriesActivity.this, DocumentariesCatalogActivity.class).putExtra("videos", (Parcelable) videoSearchResult);
 startActivity(intent);

to this:

 Intent intent =  new Intent(getApplicationContext(), VideosCatalogActivity.class);
                    intent.putExtra("com.myapp.entities.VideoEntity" , videoSearchResult);
                    startActivity(intent);

finally to retrieve the list in the other activity i used this:

Bundle bundle =  getIntent().getExtras();
    videosList = bundle.getParcelableArrayList("com.myapp.entities.VideoEntity" );
rainman
  • 2,551
  • 6
  • 29
  • 43
-1

Try this, convert the object to a byte[], pass it through the intent as normal. Then convert it back into an object.

Convert from object to byte array

// toByteArray and toObject are taken from: http://scr4tchp4d.blogspot.co.uk/2008/07/object-to-byte-array-and-byte-array-to.html
public static byte[] toByteArray(Object obj) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        bytes = bos.toByteArray();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (bos != null) {
            bos.close();
        }
    }
    return bytes;
}

Use this to create byte array to put in intent.

Intent intent =  new Intent(DocumentariesCategoriesActivity.this, DocumentariesCatalogActivity.class)
intent.putExtra("videos", toByteArray(videoSearchResult));
startActivity(intent);

Then in the next Activity, use this method.

public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
    Object obj = null;
    ByteArrayInputStream bis = null;
    ObjectInputStream ois = null;
    try {
        bis = new ByteArrayInputStream(bytes);
        ois = new ObjectInputStream(bis);
        obj = ois.readObject();
    } finally {
        if (bis != null) {
            bis.close();
        }
        if (ois != null) {
            ois.close();
        }
    }
    return obj;
}

To convert your byte array back into your object by casting.

Intent intent = getIntent();
List<VideoEntity> videoSearchResult = (List<VideoEntity>) intent.getByteArrayExtra("videos");

You could tidy it up by putting both convert functions into their own class. But make sure it works first.

Sam
  • 454
  • 4
  • 18
  • 2
    Why is this a bad answer? If you down vote it, leave a comment explaining why. This doesn't help anyone. – Sam Sep 05 '16 at 12:21
  • Not my downvote but your solution is not the best (not sure if it works, haven't tried). It would be better (in terms of OOP) that the object `VideoEntity` implements either `Parcelable` or `Serializable`, and this is how it is done in Android. – Sufian Sep 27 '16 at 10:27