答案 0 :(得分:1)
1-下载:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
//在另一个线程中解析数据快照。
}catch (Exception e){
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// =========================== 2-解析: HashMap d =(HashMap)dataSnapshot.getValue(); HashMap users =((HashMap)d.get(" users"));
3-更新ListView / EditText或使用用户(Hashmap)
4-更新:
String nameString= nameEditText.getText().toString();
FirebaseDatabase database = FirebaseDatabase.getInstance();
//获取需要更新的用户的数据库引用。
DatabaseReference databaseReference = database.getReference().child("users").child(userId);
Map<String, Object> taskMap = new HashMap<String, Object>();
taskMap.put("name", nameString);
databaseReference.updateChildren(taskMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d("", "task is: " + task);
if(task.isSuccessful()){
Toast.makeText(getActivity(),"Commit is successfull", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getActivity(),"Commit is failed", Toast.LENGTH_LONG).show();
}
}
});
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object value = dataSnapshot.getValue();
Log.d("", "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to save value.", error.toException());
}
});