[英]How Can I delete an entry from realtime database using onSwipe on a recyclerview
I am looking to delete an entry using onSwipe, I have this code below that works to swipe but it does not delete from firebase, when I swipe the app crashes.我想使用 onSwipe 删除一个条目,我在下面有这段代码可以滑动,但当我滑动应用程序崩溃时,它不会从 firebase 中删除。 see my code below and the logcat error.
请参阅下面的代码和 logcat 错误。
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
expenses.remove(viewHolder.getAdapterPosition());
adapter.notifyDataSetChanged();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child(postId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ExpensesActivity.this, "Expenditure deleted!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ExpensesActivity.this, "Something went wrong, try again" + task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
}
This is the logcat message;这是 logcat 消息;
2022-03-21 14:44:23.365 22734-22734/com.delossantos.expensapp D/AndroidRuntime: Shutting down VM
2022-03-21 14:44:23.366 22734-22734/com.delossantos.expensapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.delossantos.expensapp, PID: 22734
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
at com.delossantos.expensapp.ExpensesActivity$6.onSwiped(ExpensesActivity.java:270)
at androidx.recyclerview.widget.ItemTouchHelper$4.run(ItemTouchHelper.java:712)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Looks like your getUid()
is null
.看起来你的
getUid()
是null
。 You can use below codes to check if the user is currenty logged in or not.您可以使用以下代码检查用户当前是否登录。
final FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
if (fUser != null && postId != null) {
final String userUid = fUser.getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(userUid);
reference.child(postId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ExpensesActivity.this, "Expenditure deleted!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ExpensesActivity.this, "Something went wrong, try again" + task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
//User is missing, do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.