gpt4 book ai didi

java - 无法将 Parcelable 放入 bundle 中

转载 作者:行者123 更新时间:2023-12-02 02:04:31 29 4
gpt4 key购买 nike

我的问题很简单。我想通过包和 Parcelable 将对象列表从 Activity 传递到 Fragment。

基本上,我从 MainActivity 进行一个简单的改造调用,获取对象列表作为答案,然后将其传递给 fragment 。

我已经为我的对象类实现了 Parcelable,但它不起作用。

public class Result implements Parcelable {

// Variables
@SerializedName("geometry")
@Expose
private Geometry geometry;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("opening_hours")
@Expose
private OpeningHours openingHours;
@SerializedName("photos")
@Expose
private List<Photo> photos = new ArrayList<Photo>();
@SerializedName("place_id")
@Expose
private String placeId;
@SerializedName("rating")
@Expose
private Double rating;
@SerializedName("reference")
@Expose
private String reference;
@SerializedName("scope")
@Expose
private String scope;
@SerializedName("types")
@Expose
private List<String> types = new ArrayList<String>();
@SerializedName("vicinity")
@Expose
private String vicinity;
@SerializedName("price_level")
@Expose
private Integer priceLevel;

// Constructor
public Result(Geometry geometry, String icon, String id, String name, OpeningHours openingHours, List<Photo> photos, String placeId, Double rating, String reference, String scope, List<String> types, String vicinity, Integer priceLevel) {
this.geometry = geometry;
this.icon = icon;
this.id = id;
this.name = name;
this.openingHours = openingHours;
this.photos = photos;
this.placeId = placeId;
this.rating = rating;
this.reference = reference;
this.scope = scope;
this.types = types;
this.vicinity = vicinity;
this.priceLevel = priceLevel;
}

// Getters & Setters
/**
*
* @return
* The geometry
*/
public Geometry getGeometry() {
return geometry;
}

/**
*
* @param geometry
* The geometry
*/
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}

/**
*
* @return
* The icon
*/
public String getIcon() {
return icon;
}

/**
*
* @param icon
* The icon
*/
public void setIcon(String icon) {
this.icon = icon;
}

/**
*
* @return
* The id
*/
public String getId() {
return id;
}

/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}

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

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

/**
*
* @return
* The openingHours
*/
public OpeningHours getOpeningHours() {
return openingHours;
}

/**
*
* @param openingHours
* The opening_hours
*/
public void setOpeningHours(OpeningHours openingHours) {
this.openingHours = openingHours;
}

/**
*
* @return
* The photos
*/
public List<Photo> getPhotos() {
return photos;
}

/**
*
* @param photos
* The photos
*/
public void setPhotos(List<Photo> photos) {
this.photos = photos;
}

/**
*
* @return
* The placeId
*/
public String getPlaceId() {
return placeId;
}

/**
*
* @param placeId
* The place_id
*/
public void setPlaceId(String placeId) {
this.placeId = placeId;
}

/**
*
* @return
* The rating
*/
public Double getRating() {
return rating;
}

/**
*
* @param rating
* The rating
*/
public void setRating(Double rating) {
this.rating = rating;
}

/**
*
* @return
* The reference
*/
public String getReference() {
return reference;
}

/**
*
* @param reference
* The reference
*/
public void setReference(String reference) {
this.reference = reference;
}

/**
*
* @return
* The scope
*/
public String getScope() {
return scope;
}

/**
*
* @param scope
* The scope
*/
public void setScope(String scope) {
this.scope = scope;
}

/**
*
* @return
* The types
*/
public List<String> getTypes() {
return types;
}

/**
*
* @param types
* The types
*/
public void setTypes(List<String> types) {
this.types = types;
}

/**
*
* @return
* The vicinity
*/
public String getVicinity() {
return vicinity;
}

/**
*
* @param vicinity
* The vicinity
*/
public void setVicinity(String vicinity) {
this.vicinity = vicinity;
}

/**
*
* @return
* The priceLevel
*/
public Integer getPriceLevel() {
return priceLevel;
}

/**
*
* @param priceLevel
* The price_level
*/

public void setPriceLevel(Integer priceLevel) {
this.priceLevel = priceLevel;
}

// Parcelable

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(this.geometry, flags);
dest.writeString(this.icon);
dest.writeString(this.id);
dest.writeString(this.name);
dest.writeParcelable(this.openingHours, flags);
dest.writeTypedList(this.photos);
dest.writeString(this.placeId);
dest.writeValue(this.rating);
dest.writeString(this.reference);
dest.writeString(this.scope);
dest.writeStringList(this.types);
dest.writeString(this.vicinity);
dest.writeValue(this.priceLevel);
}

protected Result(Parcel in) {
this.geometry = in.readParcelable(Geometry.class.getClassLoader());
this.icon = in.readString();
this.id = in.readString();
this.name = in.readString();
this.openingHours = in.readParcelable(OpeningHours.class.getClassLoader());
this.photos = in.createTypedArrayList(Photo.CREATOR);
this.placeId = in.readString();
this.rating = (Double) in.readValue(Double.class.getClassLoader());
this.reference = in.readString();
this.scope = in.readString();
this.types = in.createStringArrayList();
this.vicinity = in.readString();
this.priceLevel = (Integer) in.readValue(Integer.class.getClassLoader());
}

public static final Parcelable.Creator<Result> CREATOR = new Parcelable.Creator<Result>() {
@Override
public Result createFromParcel(Parcel source) {
return new Result(source);
}

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

以及MainActivity方法调用(简化)

public void initRetrofitandCall(double latitude, double longitude, int PROXIMITY_RADIUS) {
GoogleApiInterface service = GoogleMapsClient.getClient().create(GoogleApiInterface.class);
Call<Example> call = service.getNearbyRestaurants("restaurant", latitude + "," + longitude, PROXIMITY_RADIUS);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
// Response List (working as intended)
List<Result> listTest = response.body().getResults();
Log.w("Nearby Restaurants", new GsonBuilder().setPrettyPrinting().create().toJson(listTest));

// Create bundle and put listTest in it (not working)
Bundle bundle = new Bundle();
bundle.putParcelable("valuesArray", listTest);

try {

} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}

@Override
public void onFailure(Call<Example> call, Throwable t) {
Log.d("onFailure", t.toString());
}

});

}

bundle.putParcelable("valuesArray", listTest);说“需要错误的第二个参数类型 android.os.Parcelable

我的所有模型类都实现了 Parcelable,我只显示 Result.class,以免帖子太长。我也尝试过使用可序列化的方式,并且遇到了同样的问题。好像 Parcelable 或 Serialized 没有在我的模型类中实现。

非常感谢您为我提供的任何帮助。

最佳答案

您需要使用ParcelableArrayList将列表放入包中

bundle.putParcelableArrayList("valuesArray", listTest);

关于java - 无法将 Parcelable 放入 bundle 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51017202/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com