问题
Hello i try to pass object from Fragment to Activity like next:
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. 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());
}
}
}
回答1:
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
回答2:
Your problem is in your implementation of Parcelable
-- particularly your Parcel
constructor:
public Company(Parcel in) {
in.writeInt(uid);
in.writeString(name);
...
}
You're writing fields to the Parcel
in your constructor. This is the point where you need to read in the data from the Parcel
into the object you're creating. For example:
public Company(Parcel in) {
uid = in.readInt();
name = in.readString();
...
}
回答3:
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
private CompanyListener companyListener;
In the OnAttach
you have to instantiate it and uninstantiate it in the 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 :)
来源:https://stackoverflow.com/questions/25856267/share-parcelable-object-from-fragment-to-activity