Issue
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();
}
}
});
Solution
refer to firebase documentation and read about the function called push
, they clearly said that
push : Add to a list of data in the database. Every time you push a new node onto a list, your database generates a unique key, like messages/users//
so 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
reference.push().setValue(hashMap, new DatabaseReference.CompletionListener(){ . . . }
write :
reference.setValue(hashMap, new DatabaseReference.CompletionListener(){ . . . }
Answered By - abdo Salm
Answer Checked By - Robin (JavaFixing Admin)