Create
final String uniqueKey = dbRef.push().getKey();
databaseReference.child(tablename).child(uniqueKey).setValue(data)
Read
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// 근본
// Post post = dataSnapshot.getValue(Post.class);
// 하위 리스트 가져오기
for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
val data = dataSnapshot.getValue(return받을class.class);
list.add(data);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
databaseReference.addValueEventListener(postListener);
Update
databaseReference.child(table name).child(uniqueKey).updateChildren(Map<String, Object>);
Delete
databaseReference.child(table name).child(uniqueKey).removeValue()
Callback
create, update, delete : 작업이 끝나고 성공 실패여부를 callback으로 받을 수 있다.
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(UploadNotice.this, "Success", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(UploadNotice.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
delete는 onCompleteListener
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(UpdateTeacherActivity.this, "Deleted Successful", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(UpdateTeacherActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
Ref
'System Programming > Android' 카테고리의 다른 글
[Android/Kotlin] Kotlin extension (0) | 2021.07.08 |
---|---|
[Android/Kotlin] Room을 사용하여 로컬 데이터베이스에 데이터 저장 (0) | 2021.07.08 |
[Android/Firebase] Firebase Upload pdf (0) | 2021.07.01 |
[Android/Firebase] Firebase Database, Storage (0) | 2021.06.30 |
[Android/Java] 갤러리 열고 그리기 MediaStore, bitmap (0) | 2021.06.29 |