gpt4 book ai didi

java - Android 接收 null Parcelable 对象

转载 作者:行者123 更新时间:2023-12-02 08:50:33 27 4
gpt4 key购买 nike

我正在尝试使用 Parcelable 将对象从 RecyclerView 适配器传递到 fragment 。但是,当我尝试接收数据时, bundle 为空。我看过其他例子,但我看不出哪里出错了。

可分包类

public class Country extends BaseObservable implements Parcelable {
@SerializedName("name")
@Expose
private String name;
@SerializedName("snippet")
@Expose
private String snippet;
@SerializedName("country_id")
@Expose
private String countryId;
@SerializedName("id")
@Expose
private String id;
@SerializedName("coordinates")
@Expose
private Coordinates coordinates;
@SerializedName("images")
@Expose
private List<CountryImage> images;

protected Country(Parcel in) {
name = in.readString();
snippet = in.readString();
}

public static final Creator<Country> CREATOR = new Creator<Country>() {
@Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(snippet);

}

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

@Bindable
public String getCountryId() {
return countryId;
}

public void setCountryId(String countryId) {
this.countryId = countryId;
notifyPropertyChanged(BR.countryId);

}

@Bindable
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
notifyPropertyChanged(BR.id);

}

@Bindable
public Coordinates getCoordinates() {
return coordinates;
}

public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
notifyPropertyChanged(BR.coordinates);

}

@Bindable
public List<CountryImage> getImages() {
return images;
}

public void setImages(List<CountryImage> images) {
this.images = images;
notifyPropertyChanged(BR.images);

}

@Bindable
public String getSnippet() {
return snippet;
}

public void setSnippet(String snippet) {
this.snippet = snippet;
notifyPropertyChanged(BR.snippet);

}

@Bindable
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);

}

}

RetrofitAdapter.java

public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.MyViewHolder> implements CustomClickListener {
private List<Country> cities;
private CustomClickListener customClickListener;
private View view;


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

RetroItemBinding retroItemBinding =
DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
R.layout.retro_item, viewGroup, false);

view = viewGroup;

return new MyViewHolder(retroItemBinding);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.bindTo(cities.get(i));
myViewHolder.retroItemBinding.setItemClickListener(this);
}

@Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}

public void setCityList(ArrayList<Country> cities) {
this.cities = cities;
notifyDataSetChanged();
}

class MyViewHolder extends RecyclerView.ViewHolder {

private RetroItemBinding retroItemBinding;

public MyViewHolder(@NonNull RetroItemBinding retroItemBinding) {
super(retroItemBinding.getRoot());

this.retroItemBinding = retroItemBinding;
}

void bindTo(Country country) {
retroItemBinding.setVariable(BR.city, country);
retroItemBinding.setVariable(BR.itemClickListener, customClickListener);

retroItemBinding.executePendingBindings();

}
}

public void cardClicked(Country country) {
CountryFragment countryFragment = new CountryFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("Country", country);
countryFragment.setArguments(bundle);

((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction()
.replace(R.id.frag_container, new CountryFragment())
.commit();


}


}

我在哪里尝试接收 CountryFragment.java 中的数据

        Country country;
Bundle bundle = this.getArguments();
if (bundle != null) {
country = bundle.getParcelable("Country");
}

最佳答案

.replace(R.id.frag_container, new CountryFragment())

应该是

.replace(R.id.frag_container, countryFragment)

您正在创建第二个实例,而不是传递您设置参数的实例。

关于java - Android 接收 null Parcelable 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60802829/

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