gpt4 book ai didi

android - 从 RecyclerAdapter 上的 firebase 中删除数据

转载 作者:行者123 更新时间:2023-11-29 02:29:54 25 4
gpt4 key购买 nike

我要在我的 RecyclerViewAdapter 上删除一个带有 PopupMenu 的列表项,但是当该项目被删除时,它会保留在 Firebase 上。

This is on the onBindViewHolder on the RecyclerAdapter

holder.opcionesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.opcionesButton);
//inflating menu from xml resource
popup.inflate(R.menu.menu_edit);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit_item:
//handle menu1 click
break;
case R.id.delete_item:

tareasList.remove(position);
notifyItemRemoved(position);
Toast.makeText(context, "Tareas eliminada!", Toast.LENGTH_SHORT).show();
break;

}
return false;
}
});
//displaying the popup
popup.show();
}
});

And this is on the MainFragment

  final Query tareasQuery = mTareasDatabase.child(userId);
tareasQuery.addChildEventListener(new ChildEventListener() {

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Tareas tareas = new Tareas();
tareas.setFecha(dataSnapshot.getValue(Tareas.class).getFecha());
tareas.setDescripcion(dataSnapshot.getValue(Tareas.class).getDescripcion());
tareas.setLista(dataSnapshot.getValue(Tareas.class).getLista());
tareas.setPrioridad(dataSnapshot.getValue(Tareas.class).getPrioridad());
mTareasList.add(tareas);
mTareasAdapter.notifyDataSetChanged();

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String tareasId = dataSnapshot.getValue(Tareas.class).getTareasId();
mTareasDatabase.child(userId).child(tareasId).removeValue();
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String tareasId = dataSnapshot.getValue(Tareas.class).getTareasId();
mTareasDatabase.child(userId).child(tareasId).removeValue();

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

一切正常,只是没有从 firebase 中删除它

解决方案

同时从 firebase 和 recycler View 中删除项目

case R.id.delete_item:
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
String tareasId = tareas.getTareasId();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
DatabaseReference databaseReference = firebaseDatabase.getReference("Tareas")
.child(userId).child(tareasId);
databaseReference.removeValue();

tareasList.remove(position);
notifyItemRemoved(position);
Toast.makeText(context, "Tareas eliminada!", Toast.LENGTH_SHORT).show();
break;

最佳答案

好的,我解决了。我只需要在删除项上添加所有数据库引用,并同时从回收器 View 和数据库中删除它

 case R.id.delete_item:
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
String tareasId = tareas.getTareasId();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
DatabaseReference databaseReference = firebaseDatabase.getReference("Tareas")
.child(userId).child(tareasId);
databaseReference.removeValue();

tareasList.remove(position);
notifyItemRemoved(position);
Toast.makeText(context, "Tareas eliminada!", Toast.LENGTH_SHORT).show();
break;

关于android - 从 RecyclerAdapter 上的 firebase 中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50093494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com