首页 > 解决方案 > Recycle View 的当前帖子无法将正确的数据传输到另一个活动

问题描述

我使用回收视图来显示用户的帖子。我在回收视图的每个帖子上创建一个按钮。当用户点击这个按钮时,这篇文章的每一个数据都会被转移到另一个活动中。我的问题是当我点击一个帖子的按钮时,传输的数据不是这个帖子的数据。例如,当我点击帖子 1 的按钮时,帖子 2 的数据会转移到新活动,而不是帖子 1 的数据。谁能帮我解决这个问题?先感谢您

下面是我的回收视图适配器:

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

        }
    }
}

标签: androidandroid-recyclerview

解决方案


只需将您的clicklistenerfromonBindViewHolder方法放到ImageViewHolder类中并使用 adapterposition 方法:

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

重复相同以获取其他属性。


推荐阅读