简体   繁体   English

将可拆分对象从片段共享到活动

[英]Share Parcelable object from Fragment to Activity

Hello i try to pass object from Fragment to Activity like next: 您好,我尝试将对象从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);

And in Activity i make next: 在活动中,我接下来要做:

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

And i Try: 我尝试:

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

And in Activity: 在活动中:

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

And company is with null fields after i get it. 我知道后, company字段为空。 I am sure that object, which I sent is not with null fields. 我确定我发送的对象没有空字段。

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

} }

In this piece of code 在这段代码中

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

}

you should read values from parcel: 您应该从包裹中读取值:

uid = in.readInt();

and so on 等等

Your problem is in your implementation of Parcelable -- particularly your Parcel constructor: 您的问题出在Parcelable的实现中-特别是您的Parcel构造函数:

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

You're writing fields to the Parcel in your constructor. 的字段Parcel在你的构造。 This is the point where you need to read in the data from the Parcel into the object you're creating. 在这一点上,您需要将数据从Parcel 入正在创建的对象中。 For example: 例如:

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

You can create a listener to communicate between your fragment and your activity : 您可以创建一个侦听器,以在片段和活动之间进行通信:

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

Next, in your Fragment you have to create an instance of this listener 接下来,您必须在Fragment中创建此侦听器的实例

private CompanyListener companyListener;

In the OnAttach you have to instantiate it and uninstantiate it in the OnDetach : OnAttach您必须实例化它,并在OnDetach取消实例化它:

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

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

Now you just have to implement it in your activity. 现在,您只需要在您的活动中实施它即可。

Also you can use Otto library from Square but it's more complicated :) 您也可以使用Square的Otto库,但更为复杂:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM