注册
登录
java
在 RecyclerView 中选择并从 java 中的 firebase 中删除
返回
在 RecyclerView 中选择并从 java 中的 firebase 中删除
作者:
糖果
发布时间:
2025-04-03 08:30:35 (3天前)
''' @Override public void onBindViewHolder(@NonNull AdvertisementHolder holder, int position) { imgUrl = publishedAdvertisements.get(position).getImgUrl(); holder.petName.setText(publishedAdvertisements.get(position).getPetName()); holder.petCategory.setText(publishedAdvertisements.get(position).getPetCategory()); Picasso.get().load(publishedAdvertisements.get(position).getImgUrl()).into(holder.petImage); holder.btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { firebaseFirestore = FirebaseFirestore.getInstance(); firebaseFirestore.collection("Pets").get().addOnSuccessListener(new OnSuccessListener
() { @Override public void onSuccess(@NonNull QuerySnapshot queryDocumentSnapshots) { if (!queryDocumentSnapshots.isEmpty()) { System.out.println("bos döndü"); docId = queryDocumentSnapshots.getDocuments().get(pos).getId(); } } }); System.out.println(docId); if (docId != null) { FirebaseFirestore db = FirebaseFirestore.getInstance(); db.collection("Pets").document(docId) .delete() .addOnSuccessListener(new OnSuccessListener
() { @Override public void onSuccess(Void aVoid) { Log.d(TAG, "DocumentSnapshot successfully deleted!"); publishedAdvertisements.clear(); getPublishedAnimals(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error deleting document", e); } }); } notifyDataSetChanged(); } }); ''' 您好,我想从 firebase 中删除我的文档。但首先我需要确定文档 ID。根据这样做,我只是尝试获取文档 ID;''' docId = queryDocumentSnapshots.getDocuments().get(pos).getId(); ''' 然后,我只想删除我的文档。但是 firebase 是异步工作的,所以代码在 if 语句中不起作用。当我们第一次单击按钮时, docId 变量为 null 或者它采用之前单击的 docId 直到异步代码部分完成。
收藏
举报
2 条回复
1#
回复此人
糖果
|
2022-01-07 13-36
您应该构建您的代码,以便在响应回调中执行或触发依赖于您的异步操作的任何逻辑。 你可以这样做: @Override public void onBindViewHolder(@NonNull AdvertisementHolder holder, int position) { imgUrl = publishedAdvertisements.get(position).getImgUrl(); holder.petName.setText(publishedAdvertisements.get(position).getPetName()); holder.petCategory.setText(publishedAdvertisements.get(position).getPetCategory()); Picasso.get().load(publishedAdvertisements.get(position).getImgUrl()).into(holder.petImage); holder.btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { firebaseFirestore = FirebaseFirestore.getInstance(); firebaseFirestore.collection(“Pets”).get().addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(@NonNull QuerySnapshot queryDocumentSnapshots) { // The asynchronous operation has successfully completed // and returned a value to our ‘onSuccess()’ callback. if (!queryDocumentSnapshots.isEmpty()) { System.out.println(“bos döndü”); docId = queryDocumentSnapshots.getDocuments().get(pos).getId(); System.out.println(docId); // We can now use the value of docId. if (docId != null) { FirebaseFirestore db = FirebaseFirestore.getInstance(); db.collection(“Pets”).document(docId) .delete() .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void aVoid) { Log.d(TAG, “DocumentSnapshot successfully deleted!”); publishedAdvertisements.clear(); getPublishedAnimals(); // (1) } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, “Error deleting document”, e); } }); } // I’m not sure how your RecyclerView is set up // but I’m guessing you might want to move this call // to ‘notifyDataSetChanged()’ to the section marked (1) notifyDataSetChanged(); } } }); } }); }
编辑
登录
后才能参与评论