I want to delete this record ?
Because I don’t know what exactly it is, I want to know what it is… I expect that it is a special key for every data I upload it creates it automatically How do I delete and what is this key?
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("poll_post").child(firebaseUser.getUid());
HashMap<String, Object> hashMap = new HashMap<>();
String postid = reference.push().getKey();
hashMap.put("postid", postid);
hashMap.put("time_post", ServerValue.TIMESTAMP);
hashMap.put("stopcomment", checked);
hashMap.put("tv_question", addcomment.getText().toString());
hashMap.put("tvoption1", Answer1.getText().toString());
hashMap.put("tvoption2", Answer2.getText().toString());
hashMap.put("vote1","0");
hashMap.put("vote2", "0");
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.push().setValue(hashMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
//Problem with saving the data
if (databaseError != null) {
Toast.makeText(Write_poll.this, "Error", Toast.LENGTH_SHORT).show();
myLoadingButton.showErrorButton();
} else {
myLoadingButton.showDoneButton();
finish();
}
}
});
2
Answers
refer to firebase documentation and read about the function called
push
, they clearly said thatso that’s means that every time you are pushing to the database , a new record with a unique ID will be generated every you push and you will not be able to override a record using the function
push
as every time it’s called , it will generate a unique ID that you don’ want in your case.to avoid that , instead of
write :
If you want to delete a record from the Realtime Database, you have to create a reference that points to that node. In your particular case it would be:
If you don’t know the key, then you have to create a query to find that post based on something that uniquely identifies it, for example, the
time_post
. So here is the code:You can also attach a listener to the
removeValue()
operation to see if something goes wrong.