将Parcelable对象从Fragment共享到Activity

时间:2014-09-15 20:20:39

标签: android android-activity android-fragments parcelable

您好我尝试将对象从Fragment传递给Activity,如下所示:

 Bundle bundle1 = new Bundle();
 bundle1.putParcelable("company", companies.get(i));
 Intent intent = new Intent(getActivity(), CompanyInfoActivity.class);
 intent.putExtras(bundle1);
 getActivity().startActivity(intent);

在活动中我做下一个:

 Intent intent = getIntent();
 Bundle bundle = intent.getExtras();
 company = bundle.getParcelable("company");

我试试:

 Intent intent = new Intent(getActivity(), CompanyInfoActivity.class);
            intent.putExtra("company",companies.get(i));
            getActivity().startActivity(intent);

在活动中:

    Intent intent = getIntent();
    company = intent.getParcelableExtra("company");
    tvName.setText(company.getName()); 

我得到它之后company是空字段。我确信我发送的对象不是空字段。

My Company.class

public class Company implements Parcelable {

private int uid;
@SerializedName("field_user_org_name_value")
private String name;
@SerializedName("field_user_org_address_value")
private String address;
@SerializedName("field_user_org_category_tid")
private int category;
@SerializedName("field_user_org_town_tid")
private int cityId;
@SerializedName("image")
private String imageUrl;
private int online;
@SerializedName("field_user_org_description_value")
private String description;
@SerializedName("field_user_org_phone_value")
private String mobPhone;
@SerializedName("field_user_org_stac_phone_value")
private String phone;

double lat;
double lng = 0;
double distance;


public double getDistance() {
    return distance;
}

public void setDistance(double distance) {
    this.distance = distance;
}

public double getLng() {
    return lng;
}

public void setLng(double lng) {
    this.lng = lng;
}

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public String getDescription() {
    return description;
}

public String getMobPhone() {
    return mobPhone;
}

public String getPhone() {
    return phone;
}

public int getOnline() {
    return online;
}


public Company(int id, String name, String address, int category, int cityId, String imageUrl, int online, String description, String mobPhone, String phone, Double lat, Double lng, Double distance) {
    this.uid = id;
    this.name = name;
    this.address = address;
    this.category = category;
    this.cityId = cityId;
    this.imageUrl = imageUrl;
    this.online = online;
    this.description = description;
    this.mobPhone = mobPhone;
    this.phone = phone;
    this.lat = lat;
    this.lng = lng;
    this.distance = distance;
}

public Company(Parcel in) {
    in.writeInt(uid);
    in.writeString(name);
    in.writeString(address);
    in.writeInt(category);
    in.writeInt(cityId);
    in.writeString(imageUrl);
    in.writeInt(online);
    in.writeString(description);
    in.writeString(mobPhone);
    in.writeString(phone);
    in.writeDouble(lat);
    in.writeDouble(lng);
    in.writeDouble(distance);

}

public int getUid() {
    return uid;
}

public String getName() {
    return name;
}

public String getAddress() {
    return address;
}

public int getCategory() {
    return category;
}

public int getCityId() {
    return cityId;
}

public String getImageUrl() {
    return imageUrl;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(uid);
    dest.writeString(name);
    dest.writeString(address);
    dest.writeInt(category);
    dest.writeInt(cityId);
    dest.writeString(imageUrl);
    dest.writeInt(online);
    dest.writeString(description);
    dest.writeString(mobPhone);
    dest.writeString(phone);
    dest.writeDouble(lat);
    dest.writeDouble(lng);
    dest.writeDouble(distance);

}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

    @Override
    public Object createFromParcel(Parcel source) {
        return new Company(source);
    }

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


public static class CustomComparator implements Comparator<Company> {
    @Override
    public int compare(Company o1, Company o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

public static class CustomComparatorDistance implements Comparator<Company> {
    @Override
    public int compare(Company o1, Company o2) {
        return Double.compare(o1.getDistance(), o2.getDistance());
    }
}

}

3 个答案:

答案 0 :(得分:2)

在这段代码中

public Company(Parcel in) {
    in.writeInt(uid);
    in.writeString(name);
    in.writeString(address);
    in.writeInt(category);
    in.writeInt(cityId);
    in.writeString(imageUrl);
    in.writeInt(online);
    in.writeString(description);
    in.writeString(mobPhone);
    in.writeString(phone);
    in.writeDouble(lat);
    in.writeDouble(lng);
    in.writeDouble(distance);

}

您应该从包裹中读取值:

uid = in.readInt();

等等

答案 1 :(得分:1)

您的问题出在Parcelable的实施中 - 尤其是您的Parcel构造函数:

public Company(Parcel in) {
    in.writeInt(uid);
    in.writeString(name);
    ...
}

您正在构造函数中将字段写入 Parcel。您需要Parcel中的数据读入您正在创建的对象中。例如:

public Company(Parcel in) {
    uid = in.readInt();
    name = in.readString();
    ...
}

答案 2 :(得分:-2)

您可以创建一个监听器来在您的片段和您的活动之间进行通信:

public interface CompanyListener
{
    public void sendCompany(Company company);
}

接下来,在Fragment中,您必须创建此侦听器的实例

private CompanyListener companyListener;

OnAttach中你必须实例化它并在OnDetach中取消实例化:

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
    this.companyListener = (YourActivity) activity;
}

@Override
public void onDetach()
{
    super.onDetach();
    this.companyListener = null;
}

现在你只需要在你的活动中实现它。

你也可以使用Otto library from Square,但它更复杂:)