我使用回收站视图来显示用户的帖子。我在回收视图的每个帖子上创建一个按钮。当用户单击此按钮时,该帖子的所有数据将被转移到另一个活动。我的问题是,当我单击帖子的按钮时,转移的数据不是该帖子的数据。例如,当我单击帖子1的按钮时,帖子2的数据将转移到新活动中,而不是帖子1的数据。有人可以帮助我解决此问题吗?预先谢谢你
以下是我的Recycle View适配器:
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);
}
}
}
答案 0 :(得分:2)
只需将clicklistener
方法中的onBindViewHolder
放到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);
}
});
重复此操作以获取其他属性。
答案 1 :(得分:2)
您正在全局初始化字符串,因此它们将使用列表中的最后一个值在ImageViewHolder中对其进行初始化
公共类ImageViewHolder扩展了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;
答案 2 :(得分:0)
酷!在您的适配器中,只需执行以下操作
科特琳
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;
}
答案 3 :(得分:0)
当您单击“回收者”视图中最后一个项目上的按钮时,它是否崩溃?如果是这样,则可能是您的索引超出范围。