Receiving null Parcelable object Android

bateler8 :

I'm trying to pass an object from my RecyclerView adapter to a fragment using parcelable. However when I try to receive the data, the bundle is null. I've looked at other examples, but I can't see where I'm going wrong.

Parcelable class

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();


    }


}

Where I receive attempt to receive the data in CountryFragment.java

        Country country;
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            country = bundle.getParcelable("Country");
        }
Ryan M :
.replace(R.id.frag_container, new CountryFragment())

should be

.replace(R.id.frag_container, countryFragment)

You are creating a second instance instead of passing the one you set the arguments on.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350325&siteId=1