如何将一个对象列表从一个Activity传递到另一个Activity?

时间:2016-09-03 22:46:34

标签: android android-intent android-activity

我将尝试尽可能地解释,我正在开发一个Android应用程序,在Java类中我有一些数据(对象)列表,我想将此列表转移到另一个Java类(Activity),按顺序在此课程中使用此列表的数据。

这是我的代码:

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();
    }

正如您在代码中看到的,我要将列表传输到的活动是DocumentariesCatalogActivity。我不知道这是否是传递它的正确方法,如果是,那么我的问题是如何捕获VideoEntity类中的DocumentariesCatalogActivity列表?

我看到了一些类似的问题,但只有String类型。

2 个答案:

答案 0 :(得分:2)

我可以解决它在我的对象类VideoEntity中实现Parcelable接口:

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];
    }
};

}

在我要转移列表的活动中,我改变了这个:

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

到此:

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

最后检索我用过的其他活动中的列表:

Bundle bundle =  getIntent().getExtras();
    videosList = bundle.getParcelableArrayList("com.myapp.entities.VideoEntity" );

答案 1 :(得分:-1)

试试这个,将对象转换为byte [],正常传递意图。然后将其转换回对象。

从对象转换为字节数组

// 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;
}

使用它来创建字节数组以放入意图。

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

然后在下一个Activity中,使用此方法。

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;
}

通过强制转换将字节数组转换回对象。

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

你可以将两个转换函数放入自己的类中来整理它。但要确保它先行。

相关问题