Мой вопрос довольно прост.Я хочу передать Список объектов из Деятельности во Фрагмент благодаря связке и Parcelable.
По сути, я делаю простой вызов дооснащения из MainActivity, получаю список объектов в качестве ответа, а затем передаю его фрагменту.
Я реализовал 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);говорит "требуется неправильный 2-й тип аргумента android.os.Parcelable
Все мои классы моделей реализуют Parcelable, я только показываю Result.class, чтобы не делать пост слишком длинным. Я такжепробовал с Serializable способом, и я получаю такие же проблемы. Как будто Parcelable или Serializable не реализованы в моих модельных классах.
Большое спасибо за любую помощь, чем вы можете мне предоставить.