0

I use recycle view to show posts of users. I create a button on each post of Recycle view. When user click on this button, every data of this post will be transferred to another activity. My problem is that when I click on the button of a post, the tranferred data is not the data of this post. For example, when I click the button of post 1, the data of post 2 is transferred to new activity instead of the data of post 1. Can anyone help me to solve this problem? Thank you in advance

Below is my Recycle View adapter:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ImageViewHolder> {

    private Context context;
    private List<Upload> uploads;
    Upload uploadCurrent;

    private String userEmail, locationName, locationType, locationAddress,
            userComment, downloadUrl, userLatitude, userLongitude;

    public CustomAdapter(Context context, List<Upload> uploads) {
        this.context = context;
        this.uploads = uploads;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.custom_view, parent, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        uploadCurrent = this.uploads.get(position);

        userEmail = uploadCurrent.getUserEmail();
        locationName = uploadCurrent.getLocationName();
        locationType = uploadCurrent.getLocationType();
        locationAddress = uploadCurrent.getLocationAddress();
        userComment = uploadCurrent.getUserComment();
        downloadUrl = uploadCurrent.getDownloadUrl();
        userLatitude = uploadCurrent.getUserLatitude();
        userLongitude = uploadCurrent.getUserLongitude();

        holder.emailCustom.setText(userEmail);
        holder.nameCustom.setText(locationName);
        holder.commentCustom.setText("Review: " + userComment );
        holder.typeCustom.setText("Type: "+ locationType );
        holder.addressCustom.setText("Address: " + locationAddress);
        Picasso.get().load(downloadUrl).fit().centerCrop().into(holder.imageCustom);

        //handle button
        holder.saveCustomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(context, SaveActivity.class);
                intent.putExtra("adap_name", locationName);
                intent.putExtra("adap_address", locationAddress);
                intent.putExtra("adap_type", locationType);
                intent.putExtra("adap_comment", userComment);
                intent.putExtra("adap_image", downloadUrl);
                intent.putExtra("adap_latitude", userLatitude);
                intent.putExtra("adap_longitude", userLongitude);

                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return this.uploads.size(); //how many items in our uploads list
    }


    public class ImageViewHolder extends RecyclerView.ViewHolder {

        public TextView emailCustom;
        public TextView nameCustom;
        public TextView commentCustom;
        public TextView typeCustom;
        public TextView addressCustom;
        public ImageView imageCustom;
        public Button saveCustomButton;


        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);

            emailCustom = itemView.findViewById(R.id.emailCustom);
            nameCustom = itemView.findViewById(R.id.nameCustom);
            typeCustom = itemView.findViewById(R.id.typeCustom);
            addressCustom = itemView.findViewById(R.id.addressCustom);
            commentCustom = itemView.findViewById(R.id.commentCustom);
            imageCustom = itemView.findViewById(R.id.imageCustom);
            saveCustomButton = itemView.findViewById(R.id.saveCustomButton);

        }
    }
}
Vy Tran
  • 35
  • 1
  • 7

4 Answers4

2

Just put your clicklistener from onBindViewHolder method to ImageViewHolder class and make use of adapterposition method:

    holder.saveCustomButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            uploadCurrent = uploads.get(getAdapterPosition());
            Intent intent = new Intent(context, SaveActivity.class);
            intent.putExtra("adap_name", uploadCurrent.getlocationName());
            context.startActivity(intent);
        }
    });

Repeat same for getting other attributes.

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
0

Cool! In your adapter, just do the below things

Kotlin

override fun getItemId(position: Int): Long {
return position.toLong()}



override fun getItemViewType(position: Int): Int {
 return position}

Java

@Override
public Long getItemId(int position) {
 return position;
}

@Override
public int getItemViewType(int position) {
 return position;
}
IamVariable
  • 408
  • 6
  • 23
  • You may try passing an object instead of passing single values, https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android may help you. – IamVariable Jun 27 '19 at 04:09
0

Does it crash when you click the button on the last item in the recycler view? If so then it could be that your index is out of bound.

Haider Malik
  • 1,581
  • 1
  • 20
  • 23
0

you are initialize string globally so they are taking last value from list initialize them in ImageViewHolder that will work for you

public class ImageViewHolder extends RecyclerView.ViewHolder {

    public TextView emailCustom;
    public TextView nameCustom;
    public TextView commentCustom;
    public TextView typeCustom;
    public TextView addressCustom;
    String userEmail, locationName, locationType, locationAddress,
        userComment, downloadUrl, userLatitude, userLongitude;
Rahul ShaRma
  • 99
  • 4
  • 8
  • it's not about public they are taking value from the last index of the list and store them you have to reinitialize them for again use @VyTran – Rahul ShaRma Jun 27 '19 at 05:04