我的问题很简单。由于捆绑和可打包,我想将对象列表从活动传递到片段。
基本上,我从MainActivity进行了一个简单的改造调用,获取对象列表作为答案,然后将其传递给片段。
我已经对我的对象类实现了可分割的,但是它不起作用。
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或Serializable。
非常感谢您提供的所有帮助。
答案 0 :(得分:2)
您需要使用 ParcelableArrayList 将列表放入包中
bundle.putParcelableArrayList("valuesArray", listTest);
答案 1 :(得分:0)
将此添加到build.gradle
compile 'com.google.code.gson:gson:2.8.1'
使用Gson将您的Arraylist转换为json,并将json字符串作为参数传递给片段。
Bundle bundle = new Bundle();
bundle.putString("valuesArray", new Gson().toJson(listTest));
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
然后像这样检索片段中的结果列表,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<Result> result = new Gson().fromJson(getArguments().getString("valuesArray"),
new TypeToken<List<Result>>(){}.getType());
return inflater.inflate(R.layout.fragment, container, false);
}
答案 2 :(得分:0)
为我工作:
Intent intent = new Intent(YourActivity.this, YourFragment.class);
intent.putParcelableArrayListExtra(Utils.LIST_TEST, (ArrayList<? extends Parcelable>) listTest);