Firebase Realtime Database Queries and Listeners
Firebase Realtime Database Queries and Listeners
In this post, we will learn about Firebase Realtime Database Queries and Listeners.
Queries are used to sort our data and categorize our data.
Some Queries of Firebase Realtime Database are.
- Limit to First Query (To get the first specific items from the list).
- Limit to Last Query (To get last specific items from the list).
- Order by Child and Start at Query (Returns Specific child with a specific name also).
- Order by Child and equal to Query (Returns Specific child equal to a specific name also).
Listeners are used to getting data and set that data on the list view.
- Child Event Listener (This event occurs whenever a change in the child occurs).
- Single value event listener (This event occurs only when the activity starts).
- Value event Listener (This event occurs whenever a change occurs in the database).
Before we do that, you should cover all the topic of Firebase Realtime Database CRUD operations and setup your list view and custom list from here.
You can also see our video playlist of Firebase Realtime Database CRUD operations from here.
//everytime when an event occurs in Firebase Database, this listener is called..
FirebaseDatabase.getInstance().getReference("Student").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Toast.makeText(MainActivity.this, "Child Added", Toast.LENGTH_SHORT).show();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Toast.makeText(MainActivity.this, "Child changed", Toast.LENGTH_SHORT).show();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
Toast.makeText(MainActivity.this, "Child removed", Toast.LENGTH_SHORT).show();
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Query query = FirebaseDatabase.getInstance().getReference("Student/").orderByChild("rollno").startAt("7");
// Query query = FirebaseDatabase.getInstance().getReference("Student/").orderByChild("name").startAt("S");
// Query query = FirebaseDatabase.getInstance().getReference("Student/").limitToFirst(4);
// Query query = FirebaseDatabase.getInstance().getReference("Student/").limitToLast(4);
// Query query = FirebaseDatabase.getInstance().getReference("Student/").orderByChild("name").equalTo("Harpreet");
//if you use order by child, you must use .index on in firebase rules,
// because without indexon it downloads all data and give you the required data (consumes more bandwidth)
// with .indexon it only downloads required data..
//valueeventlistener occurs everytime when a data changed in database,
// if you don't want to update in realtime use single value event listener
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
studentlist.clear(); // because everytime when data updates in your firebase database it creates the list with updated items
// so to avoid duplicate fields we clear the list everytime
if(dataSnapshot.exists())
{
for(DataSnapshot studentsnapshot : dataSnapshot.getChildren())
{
StudentModel studentModel = studentsnapshot.getValue(StudentModel.class);
studentlist.add(studentModel);
}
Student_ListAdaptor adaptor = new Student_ListAdaptor(MainActivity.this,studentlist);
studentListView.setAdapter(adaptor);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Subscribe to Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram
No comments