Retrieving data from a Firebase Realtime database in Android

I am new to Firebase and NoSQL. I have an Android Demo with the Autocomplete City text box in which I want to populate the cities that I have from my Firebase database as I type.

{ "cities":{ "Guayaquil":true, "Gualaceo":true, "Quito":true, "Quevedo":true, "Cuenca":true, "Loja":true, "Ibarra":true, "Manta":true } } 

This is what I still have.

How can I get from DB cities that start with a letter (keyboard input)? If I start typing "G", I want to get "Guayaquil" and "Gualaceo".

If I use orderByValue , an empty snapshot is always returned.

If I use orderByKey , return the whole list.

  Query citiesQuery = databaseRef.child("cities").startAt(input).orderByValue(); citiesQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { List<String> cities = new ArrayList<String>(); for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { cities.add(postSnapshot.getValue().toString()); } 

Note. . If you can recommend a better data structure, please.

+5
source share
3 answers

@NicholasChen identified the problem. But here is how you implement using the SDK 3.x:

 DatabaseReference cities = databaseRef.child("cities") Query citiesQuery = cities.orderByKey().startAt(input).endAt(input+"\uf8ff"); citiesQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { List<String> cities = new ArrayList<String>(); for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { cities.add(postSnapshot.getValue().toString()); } 

Starting with user input and ending with the last line starting with user input, you get all the relevant elements

For relatively short lists of items, Ryan's approach will also work fine. But the aforementioned Firebase request will filter the server side.

Update

I just ran this code:

  DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("39714936"); String input = "G"; DatabaseReference cities = databaseRef.child("cities"); Query citiesQuery = cities.orderByKey().startAt(input).endAt(input + "\uf8ff"); citiesQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { List<String> cities = new ArrayList<String>(); for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) { cities.add(postSnapshot.getValue().toString()); } System.out.println(cities); } @Override public void onCancelled(DatabaseError databaseError) { } }); 

And it is printed:

True

True

So clearly corresponds to two cities.

You can not test my database: https://stackoverflow.firebaseio.com/39714936

+6
source

Try something similar to iterate over the children in the cities snapshot and add all the cities to the ArrayList from Strings .

 ArrayList<String> cityList = new ArrayList<>(); databaseRef.child("cities").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { cityList.clear(); for (DataSnapshot data : dataSnapshot.getChildren()){ cityList.add(data.getKey); } } @Override public void onCancelled(DatabaseError databaseError) { Log.w(TAG, "getUser:onCancelled", databaseError.toException()); // ... } }); 

Editing this paragraph for clarity:

This will lead to the fact that all your cities will be read in the program memory so that you can use this data to display cities for the user. If the list of cities changes, the data that the user sees will also be displayed. If the user is not connected to the network, this will not work. This puts a real-time online listener in the database.

The logic in my mind is something like:

  • Set a value listener in the text box.
  • When the user types, create a view that displays all the elements in the list of arrays that begin with the same substring that was printed.
  • Of course, handle arrayIndex errors.

Hope this helps you on the right track. I am sure that there are other ways to implement it, but this is what I personally would do. If you need help with a code to display the correct cities, start a chat with me and I can brainstorm with you.

+1
source

Of course, OrderByValue returns nothing, because you have a boolean.

you can use startAt and endAt methods. (Below is the Firebase 2.0 code)

 var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs"); ref.orderByKey().startAt("b").endAt("b\uf8ff").on("child_added", function(snapshot) { console.log(snapshot.key()); }); 

You can find more on the Firebase 3 documentation site here .

What Ryan did was right. However, you must implement startAt in dataSnapshot to make sure your live search is working.

0
source

All Articles