FirebaseRealtimeDatabase class in Android
Firebase Realtime Database class with CRUD operations in an Android application
public class FirebaseRealtimeDatabase {
private DatabaseReference databaseReference;
public FirebaseRealtimeDatabase() {
databaseReference = FirebaseDatabase.getInstance().getReference();
}
public void createData(String node, Object data, final OnDataCreatedListener listener) {
databaseReference.child(node).setValue(data)
.addOnSuccessListener(aVoid -> {
listener.onDataCreated();
})
.addOnFailureListener(e -> {
listener.onDataCreateFailed(e.getMessage());
});
}
public void readData(String node, final OnDataReadListener listener) {
databaseReference.child(node).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listener.onDataRead(dataSnapshot.getValue());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
listener.onDataReadFailed(databaseError.getMessage());
}
});
}
public void updateData(String node, Object data, final OnDataUpdatedListener listener) {
databaseReference.child(node).setValue(data)
.addOnSuccessListener(aVoid -> {
listener.onDataUpdated();
})
.addOnFailureListener(e -> {
listener.onDataUpdateFailed(e.getMessage());
});
}
public void deleteData(String node, final OnDataDeletedListener listener) {
databaseReference.child(node).removeValue()
.addOnSuccessListener(aVoid -> {
listener.onDataDeleted();
})
.addOnFailureListener(e -> {
listener.onDataDeleteFailed(e.getMessage());
});
}
public interface OnDataCreatedListener {
void onDataCreated();
void onDataCreateFailed(String message);
}
public interface OnDataReadListener {
void onDataRead(Object data);
void onDataReadFailed(String message);
}
public interface OnDataUpdatedListener {
void onDataUpdated();
void onDataUpdateFailed(String message);
}
public interface OnDataDeletedListener {
void onDataDeleted();
void onDataDeleteFailed(String message);
}
}
To use this class, create an instance of it in your activity or fragment and call the appropriate method with the required parameters. Here's an example of using this class to create data:
FirebaseRealtimeDatabase firebaseRealtimeDatabase = new FirebaseRealtimeDatabase();
String node = "my_node";
String data = "Hello, world!";
firebaseRealtimeDatabase.createData(node, data, new FirebaseRealtimeDatabase.OnDataCreatedListener() {
@Override
public void onDataCreated() {
// Data created successfully
}
@Override
public void onDataCreateFailed(String message) {
// Data creation failed, handle error
}
});
In this example, we create a new instance of the FirebaseRealtimeDatabase
class, specify a node to create data in, and specify the data to be created. Then we call the createData()
method on the FirebaseRealtimeDatabase
instance with the node
, data
, and an OnDataCreatedListener
object to handle the create success or failure. Inside the onDataCreated()
method, we can do something after the data is created successfully.