android - 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);
}
}
}
解决方案
只需将您的clicklistener
fromonBindViewHolder
方法放到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);
}
});
重复相同以获取其他属性。
推荐阅读
- javascript - 如何暂停音轨?
- excel - 工作表上的 VBA excel 事件为每个工作表打开全局
- python - 不同车速的车辆路线(谷歌或工具)
- mysql - 在 mysql 上搜索第一个下划线之前的数字
- youtube - iOS 应用上的嵌入式 YouTube 视频观看次数不计算在内
- node.js - docker alpine:autoreconf:运行aclocal失败:没有这样的文件或目录
- react-native - React Native WebView - 如何将标头中的 JWT 令牌发送到 WordPress 站点
- apache-spark - 如何在 Spark 的工作节点中读取文件?
- javascript - 如何在 JavaScript 中将两个对象合并到同一个属性中?
- r - 修复选项卡之间的 FluidRows