Yatin Yatin - 1 month ago 7
Android Question

Android Firebase, server side filtering for Recyclerview

Following is my firebase data structure

{
"ticketing" : {
"1476639649079-mahto" : {
"agentId" : 0,
"agent_status" : "1_Dispatched",
"approverId" : 0,
"contractorId" : 0,
"dateTime" : "16-10-2016 13:40 PM",
"priority" : "MEDIUM",
"requester" : {
"issue" : "issue 3",
"location" : "location 3",
"priority" : "Medium",
"shop" : "shop 3",
"site" : "site 3",
"userInfo" : {
"email" : "m@gmail.com",
"firstname" : "mahto",
"isLoggedin" : true,
"lastname" : "pancha",
"loggedin" : true,
"number" : "123123123",
"password" : "q",
"role" : "fieldagent",
"userid" : "2",
"username" : "q"
}
},
"requesterId" : 2,
"status" : "Incoming",
"ticketNumber" : "1476639649079-mahto"
},
"1476741838857-mahto" : {
"agentId" : 1,
"agent_status" : "1_Incoming",
"approverId" : 0,
"contractorId" : 0,
"dateTime" : "17-10-2016 18:03 PM",
"priority" : "HIGH",
"requester" : {
"issue" : "Description 3",
"location" : "Location 3",
"priority" : "HIGH",
"shop" : "shop 3",
"site" : "site 3",
"userInfo" : {
"email" : "m@gmail.com",
"firstname" : "mahto",
"isLoggedin" : true,
"lastname" : "pancha",
"loggedin" : true,
"number" : "123123123",
"password" : "q",
"role" : "fieldagent",
"userid" : "2",
"username" : "q"
}
},
"requesterId" : 2,
"status" : "Incoming",
"ticketNumber" : "1476741838857-mahto"
}
}
}


Database refrence

mDatabase = FirebaseDatabase.getInstance().getReference().child("ticketing");
mDatabase.orderByChild("agent_status").equalTo("1_Incoming");


and attached to recyclerview adapter

mAdapter = new FirebaseRecyclerAdapter<Ticket, AgentTicketHolder>(Ticket.class, R.layout.agent_ticket_cardview, AgentTicketHolder.class, mDatabase) { .. }


My expected result is that i will get only one node i.e 1476741838857-mahto which has agent_status value equal to 1_Incoming.

But i'am getting both the nodes in my recylerview.

enter image description here

Answer

When you call filtering methods, the Firebase SDK returns a new object for the resulting query. So you need to capture the reference to that object and pass that into the adapter:

mDatabase = FirebaseDatabase.getInstance().getReference().child("ticketing");

Query query = mDatabase.orderByChild("agent_status").equalTo("1_Incoming");

mAdapter = new FirebaseRecyclerAdapter<Ticket, AgentTicketHolder>
    (Ticket.class, R.layout.agent_ticket_cardview, AgentTicketHolder.class, query) { .. }
Comments