我有一个RecyclerView
,其中显示了来自不同子集合的很多照片。我只想在用户longclick可以编辑或删除时,从Firesbase Storage获得照片的引用,并在其上附加uid。因此,如果文档上的uid ==当前用户uid
他可以删除或编辑。
问题是我无法到达照片的路径,因为它是从不同的子集合中获取的!
请你能帮我吗?
科特琳
class ViewHolder(view: View, var my_data: Data? = null) : RecyclerView.ViewHolder(view) {
init {
view.setOnClickListener {
val intent = Intent(view.context, More::class.java)
intent.putExtra("description", my_data?.description)
intent.putExtra("photo", my_data?.photo_uri)
intent.putExtra("site", my_data?.site)
view.context.startActivity(intent)
}
view.setOnLongClickListener {
val popUp = PopupMenu(view.context, it)
popUp.inflate(R.menu.edit_delete)
popUp.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.edit -> {
true
}
R.id.delete -> {
var dp: FirebaseFirestore = FirebaseFirestore.getInstance()
dp.collection("Fraise")
.document()
.delete()
.addOnCompleteListener {
}
Toast.makeText(view.context, "Note has been deleted!", Toast.LENGTH_SHORT).show()
true
}
else -> false }
popUp.show(); true
}
答案 0 :(得分:0)
假设您保持数据库结构与其他question的位置(我为您提供了答案)并且您正在使用名为Fraise
的模型类来删除/编辑特定用户的照片,请使用以下代码:
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef.collection("Fraise").whereEqualTo("uid", uid)
query.get().addOnCompleteListener(OnCompleteListener<QuerySnapshot> { task ->
if (task.isSuccessful) {
for (document in task.result) {
val fraise = document.toObject(Fraise::class.java!!)
fraise!!.setPhoto_url(new_photo_uri)
rootRef.collection("Fraise").document(document.id).set(fraise)
}
}
})
如果没有模型类,则可以使用Map
进行更新,如下所示:
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseFirestore.getInstance()
val query = rootRef.collection("Fraise").whereEqualTo("uid", uid)
query.get().addOnCompleteListener(OnCompleteListener<QuerySnapshot> { task ->
if (task.isSuccessful) {
for (document in task.result) {
val new_photo_uri = HashMap<String, Any>()
new_photo_uri["photo_uri"] = new_photo_uri
rootRef.collection("Fraise").document(document.id).set(new_photo_uri, SetOptions.merge())
}
}
})