gabriel assenga:
Como posso atualizar um valor em uma chave existente no banco de dados firebase do android. No meu caso eu quero usuário para atualizar a chave de Votos
final long z=0;
Map<String,Object> taskMap = new HashMap<String,Object>();
taskMap.put("Votes", z);
FirebaseDatabase.getInstance().getReference()
.child("project_image").child(projectDate)
.child("Votes")
.updateChildren(taskMap);
Eu tentei os códigos acima, mas em vez disso, adicionar outro nó
FirebaseDatabase.getInstance().getReference()
.child("project_image")
.child(formattedDate)
.push().setValue(dataMap);
Os códigos acima foram usadas para inserir os dados na primeira vez
Como posso executar esta tarefa
Peter Haddad:
Você precisa acessar a chave também:
final long z=0;
Map<String,Object> taskMap = new HashMap<String,Object>();
taskMap.put("Votes", z);
FirebaseDatabase.getInstance().getReference().child("project_image")
.child(projectDate).child("-M3zLjPHM_ndBfBDLV9C")
.child("Votes")
.updateChildren(taskMap);
Para obter a chave:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("project_image").child(projectDate).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String key = datas.getKey();
final long z=0;
Map<String,Object> taskMap = new HashMap<String,Object>();
taskMap.put("Votes", z);
FirebaseDatabase.getInstance().getReference().child("project_image")
.child(projectDate).child(key)
.child("Votes")
.updateChildren(taskMap);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});