我正在制作一个Android应用,其中我使用MongoDB和NodeJs作为后端服务。我在MongoDb上保存了一些帖子,并在回收器视图中检索它们。单击回收器视图时我有一个按钮,我想要获取项目的对象ID。
我已成功在回收站视图中获取所有文档,但问题是 当我单击特定项目的按钮时,它们显示的是最近插入的文档的对象ID,而没有显示正确的项目对象ID。
这是我到目前为止所做的:
MyPostBookAdapter.java
public class MyPostedBookAdapter extends RecyclerView.Adapter<MyPostedBookAdapter.ViewHolder> {
List<PostedModel> listItem;
Activity context;
String id;
public MyPostedBookAdapter(List<PostedModel> listItem, Activity context){
this.listItem = listItem;
this.context = context;
}
@NonNull
@Override
public MyPostedBookAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.posted_book,viewGroup,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final MyPostedBookAdapter.ViewHolder viewHolder, final int i) {
final PostedModel model = listItem.get(i);
//Object Id of post
id = model.getPostId();
viewHolder.userBookName.setText(model.getPurchaseBookName());
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.openbook);
Glide.with(context).load(model.getPurchaseImage()).apply(requestOptions).into(viewHolder.userPostBook);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,id,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return listItem.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView userPostBook;
TextView userBookName;
Button delete;
public ViewHolder(@NonNull View itemView) {
super(itemView);
userPostBook = (itemView).findViewById(R.id.userPostBook);
userBookName = (itemView).findViewById(R.id.userBookName);
delete = (itemView).findViewById(R.id.delete);
}
}
}
PostedModel.java
public class PostedModel {
String purchaseImage,purchaseBookName,postId;
public PostedModel(){
}
public PostedModel(String purchaseImage, String purchaseBookName,String postId){
this.purchaseBookName = purchaseBookName;
this.purchaseImage = purchaseImage;
this.postId = postId;
}
public String getPurchaseImage() {
return purchaseImage;
}
public void setPurchaseImage(String purchaseImage) {
this.purchaseImage = purchaseImage;
}
public String getPurchaseBookName() {
return purchaseBookName;
}
public void setPurchaseBookName(String purchaseBookName) {
this.purchaseBookName = purchaseBookName;
}
public String getPostId() {
return postId;
}
public void setPostId(String postId) {
this.postId = postId;
}
}
请让我知道如何获取对应于正确项目的ObjectId。 任何帮助将不胜感激。
谢谢
答案 0 :(得分:1)
问题是
Recycler视图加载每个给定对象的对象,因此变量id具有最后一个对象的值。因此,您需要从选定的视图中获取ID。
解决方法是
@Override
public void onBindViewHolder(@NonNull final MyPostedBookAdapter.ViewHolder viewHolder, final int i) {
final PostedModel model = listItem.get(i);
//Object Id of post
id = model.getPostId();
// You need to set this id to viewHolder.
viewHolder.userBookName.setId(id);
viewHolder.userBookName.setText(model.getPurchaseBookName());
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.openbook);
Glide.with(context).load(model.getPurchaseImage()).apply(requestOptions).into(viewHolder.userPostBook);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here you can extract the id which we set at binding
int idOfView = v.userBookName.getId();
Toast.makeText(context,idOfView,Toast.LENGTH_SHORT).show();
}
});
}
希望这会对您有所帮助。