如何在android中将一个活动的数据列表发送到另一个活动

时间:2013-08-10 18:42:58

标签: android

在我的项目中,我想将一个活动的数据列表发送到另一个活动。

Bundle bundle = new Bundle();
bundle.putString("C/V", "V");
bundle.putString("mode", "vo");

1 个答案:

答案 0 :(得分:3)

最后我完成了使用我班级中的parcelable

public class Channel implements Serializable, Parcelable {

/**  */
private static final long serialVersionUID = 4861597073026532544L;

private String cid;
private String uniqueID;
private String name;
private String logo;
private String thumb;


/**
 * @return the cid
 */
public String getCid() {
    return cid;
}

/**
 * @param cid
 *            the cid to set
 */
public void setCid(String cid) {
    this.cid = cid;
}

/**
 * @return the uniqueID
 */
public String getUniqueID() {
    return uniqueID;
}

/**
 * @param uniqueID
 *            the uniqueID to set
 */
public void setUniqueID(String uniqueID) {
    this.uniqueID = uniqueID;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name
 *            the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the logo
 */
public String getLogo() {
    return logo;
}

/**
 * @param logo
 *            the logo to set
 */
public void setLogo(String logo) {
    this.logo = logo;
}

/**
 * @return the thumb
 */
public String getThumb() {
    return thumb;
}

/**
 * @param thumb
 *            the thumb to set
 */
public void setThumb(String thumb) {
    this.thumb = thumb;
}


public Channel(Parcel in) {
    super();
    readFromParcel(in);
}

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

    public Channel[] newArray(int size) {

        return new Channel[size];
    }

};

public void readFromParcel(Parcel in) {
    String[] result = new String[23];
    in.readStringArray(result);

    this.cid = result[0];
    this.uniqueID = result[1];
    this.name = result[2];
    this.logo = result[3];
    this.thumb = result[4];


}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[] { this.cid, this.uniqueID,
            this.name, this.logo, this.thumb});

}}

在AcivityA中使用这样的方式发送数据。

Bundle bundle = new Bundle();
bundle.putParcelableArrayList("channel",(ArrayList<Channel>) channels);
Intent intent = new Intent(ActivityA.this,ActivityB.class);
intent.putExtras(bundle);
startActivity(intent);

在ActivityB中使用这样来获取数据。

Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");