Reputation: 646
So, I'm wanting to perform a search on data so that it returns a list of users that match the search (kinda like how Facebook or Twitter does their searches) so I have a couple questions since this is very hard to find online. First, users enter data into a SearchView. I have set an onQueryTextListener
and am searching the Firebase database once the user hits submit. To retrieve of query of all data that begins with the string entered, I have this code (I understand it might be entirely wrong):
FirebaseDatabase.getInstance().getReference().child("uploads").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Query query = FirebaseDatabase.getInstance().getReference().orderByChild("username").startAt(s);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
My first question is this: is this a good way of retrieving all of the users that have a username that matches the specified search? This is what my database is structured as:
If this is correct, what can i do with the query? can it be treated as a hashmap of User objects? Or does it act like nothing? I'm not sure how to deal with it once I'm finished. Thanks in advance.
Upvotes: 0
Views: 325
Reputation: 80914
Try this:
DatabaseReference db=FirebaseDatabase.getInstance().getReference().child("uploads");
db.orderByChild("username").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String usernames=dataSnapshot.child("username").getValue().toString();
}
This would get you all the usernames in the database. If you want only one username then you can do this:
db.orderByChild("username").equalTo(one_username_value_or_variable).addChildEventListener(new ChildEventListener(){....}
Upvotes: 2