如何在 recyclerview 上使用 onSwipe 从实时数据库中删除条目
How Can I delete an entry from realtime database using onSwipe on a recyclerview
我想使用 onSwipe 删除一个条目,下面有这段代码可以滑动,但当我滑动应用程序崩溃时,它不会从 firebase 中删除。请参阅下面的代码和 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();
}
}
});
}
这是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.onSwiped(ExpensesActivity.java:270)
at androidx.recyclerview.widget.ItemTouchHelper.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)
enter image description here
看起来你的 getUid()
是 null
。您可以使用以下代码检查用户当前是否登录。
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
}
我想使用 onSwipe 删除一个条目,下面有这段代码可以滑动,但当我滑动应用程序崩溃时,它不会从 firebase 中删除。请参阅下面的代码和 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();
}
}
});
}
这是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.onSwiped(ExpensesActivity.java:270)
at androidx.recyclerview.widget.ItemTouchHelper.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)
enter image description here
看起来你的 getUid()
是 null
。您可以使用以下代码检查用户当前是否登录。
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
}