簡體   English   中英

Parcelable 對象在接收活動時為 Null,在其他情況下為好

[英]Parcelable object is Null on receiving activity and good in other

感謝那些願意幫助我的人!

我第一次發消息。 我被我的包裹困住了。 我嘗試在 RecyclerView 中發送一個對象 Neighbor 為一個類DetailedNeighbour 在我將變量發送到DetailedActivity.putExtra("DNeighbour", neighbour); 完全等於我想要的並且在DetailedNeighbourActivity之后等於null。

鄰居:

包 com.openclassrooms.entrevoisins.model;

/**
 * Model object representing a Neighbour
 */
public class Neighbour implements Parcelable {

    /** Identifier */
    private long id;

    /** Full name */
    private String name;

    /** Avatar */
    private String avatarUrl;

    /** Adress */
    private String address;

    /** Phone number */
    private String phoneNumber;

    /** About me */
    private String aboutMe;

    /**
     * Constructor
     * @param id
     * @param name
     * @param avatarUrl
     */
    public Neighbour(long id, String name, String avatarUrl, String address,
                     String phoneNumber, String aboutMe) {
        this.id = id;
        this.name = name;
        this.avatarUrl = avatarUrl;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.aboutMe = aboutMe;
    }

        public  Neighbour (Parcel in){   //constructor  //Protected ?
        id =in.readLong();                  //read and set saved values from parcel
        name=in.readString();
        avatarUrl=in.readString();
        address=in.readString();
        phoneNumber=in.readString();
        aboutMe=in.readString();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAboutMe() {
        return aboutMe;
    }

    public void setAboutMe(String aboutMe) {
        this.aboutMe = aboutMe;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Neighbour neighbour = (Neighbour) o;
        return Objects.equals(id, neighbour.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) { // In this method you add all your class properties to the parcel which are needed to transfer.
        parcel.writeString(name);
        parcel.writeString(phoneNumber);
        parcel.writeString(avatarUrl);
        parcel.writeString(aboutMe);
        parcel.writeString(address);
        parcel.writeLong(id);  /// for favoris ?

    }

    public static final Parcelable.Creator<Neighbour> CREATOR = new Parcelable.Creator<Neighbour>() {   ///This is the method which is used to bind everything together. Nothing much is done here.
        @Override
        public Neighbour createFromParcel(Parcel in) {
            return new Neighbour(in);
        }




        @Override
        public Neighbour[] newArray(int size) {
            return new Neighbour[size];
        }
    };
    public static Creator<Neighbour> getCREATOR() {
        return CREATOR;
    }
    @Override
    public int describeContents() {
        return 0;
    }
}

回收器視圖適配器:

包 com.openclassrooms.entrevoisins.ui.neighbor_list;

public class MyNeighbourRecyclerViewAdapter extends RecyclerView.Adapter<MyNeighbourRecyclerViewAdapter.ViewHolder> {

    private final List<Neighbour> mNeighbours;

    public MyNeighbourRecyclerViewAdapter(List<Neighbour> items) {
        mNeighbours = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_neighbour, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Neighbour neighbour = mNeighbours.get(position);
        holder.mNeighbourName.setText(neighbour.getName());
        Glide.with(holder.mNeighbourAvatar.getContext())
                .load(neighbour.getAvatarUrl())
                .apply(RequestOptions.circleCropTransform())
                .into(holder.mNeighbourAvatar);
        holder.mNeighbourName.setOnClickListener(new View.OnClickListener() { ///   Observe le clic sur bouton name
            @Override
            public void onClick(View view) {
                Intent DetailedActivity = new Intent(view.getContext(), DetailedNeighbourActivity.class);       //
                DetailedActivity.putExtra("DNeighbour", neighbour);
                view.getContext().startActivity(DetailedActivity);//
                EventBus.getDefault();//
                Log.i("DEBUG","l'utilisateur essaye d'ouvrir le détaille");
            }
        });
        holder.mDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DeleteNeighbourEvent(neighbour));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mNeighbours.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_list_avatar)
        public ImageView mNeighbourAvatar;
        @BindView(R.id.item_list_name)
        public TextView mNeighbourName;
        @BindView(R.id.item_list_delete_button)
        public ImageButton mDeleteButton;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

詳細的鄰居活動:


public class DetailedNeighbourActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_neighbour);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        Neighbour neighbour = (Neighbour) getIntent().getParcelableExtra("DNeighbour");
        String name = neighbour.getName();


        EventBus.getDefault();
        Log.i("DEBUG", "Le détaille "+ name);
    }
}

我真的不聰明! 對不起你的時間。 parcel 和 writeparcel 構造函數需要相同的變量順序。 沒關系我的代碼工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM