Firebase Class with Crud Operation
performing CRUD operations using Firebase Realtime Database in an Android app using Java:
- Create a model class for your data
javaCopy codepublic class Person {
private String id;
private String name;
private int age;
public Person() {
// Required empty constructor
}
public Person(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// Getters and setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- Initialize Firebase in your activity or fragment
public class MainActivity extends AppCompatActivity {
private FirebaseDatabase database;
private DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Firebase
database = FirebaseDatabase.getInstance();
myRef = database.getReference("people");
}
}
- Insert data
// Generate a new id for the person
String id = myRef.push().getKey();
Person person = new Person(id, "John Doe", 25);
myRef.child(id).setValue(person);
- Read data
// Read all people
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Person> people = new ArrayList<>();
for (DataSnapshot personSnapshot : dataSnapshot.getChildren()) {
Person person = personSnapshot.getValue(Person.class);
people.add(person);
}
// Do something with the list of people
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Handle database error
}
});
// Read a single person by id
String id = "person1";
myRef.child(id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Person person = dataSnapshot.getValue(Person.class);
// Do something with the person
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Handle database error
}
});
- Update data
String id = "person1";
// Create a new person object with the updated data
Person updatedPerson = new Person(id, "Jane Doe", 30);
myRef.child(id).setValue(updatedPerson);
- Delete data
String id = "person1";
myRef.child(id).removeValue();
retrieves all person data from Firebase Realtime Database:
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Person> people = new ArrayList<>();
for (DataSnapshot personSnapshot : dataSnapshot.getChildren()) {
Person person = personSnapshot.getValue(Person.class);
people.add(person);
}
// Do something with the list of people
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Handle database error
}
});
deletes all person data from Firebase Realtime Database:
myRef.removeValue(new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
// Handle the error
} else {
// The data has been removed successfully
}
}
});
inserts an ArrayList
of person model data into Firebase Realtime Database:
ArrayList<Person> people = // Your ArrayList of Person objects
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("people");
for (Person person : people) {
String key = myRef.push().getKey();
myRef.child(key).setValue(person, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
// Handle the error
} else {
// The data has been inserted successfully
}
}
});
}