Firebase Request Data

{ "random_key 1" : { "id": 0, "text": "This is text" }, "random_key 2" : { "id": 1, "text": "This is text" } } 

If I store my data as follows and I want to get node, where id is 0 . How can i do this?

The above is a child of issue , which is a child of root .

+7
android firebase firebase-database
source share
4 answers

In your case, you will need to configure the query as follows:

  DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); Query query = reference.child("issue").orderByChild("id").equalTo(0); query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { // dataSnapshot is the "issue" node with all children with id 0 for (DataSnapshot issue : dataSnapshot.getChildren()) { // do something with the individual "issues" } } } @Override public void onCancelled(DatabaseError databaseError) { } }); 
+23
source share
Answer to

@Linxy is correct, but since you will be reading a list of items from the database, it is better to use a child event listener instead of a value event listener.

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); Query query = reference.child("issue").orderByChild("id").equalTo(0); query.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { //Do something with the individual node here`enter code here` } @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(FirebaseError firebaseError) { } }); 
+1
source share

This code works for me

 mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(final DataSnapshot dataSnapshot) { for (DataSnapshot data : dataSnapshot.getChildren()) { //If email exists then toast shows else store the data on new key if (!data.getValue(User.class).getEmail().equals(email)) { mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email)); } else { Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show(); } } } @Override public void onCancelled(final DatabaseError databaseError) { } }); 
0
source share

For easy-to-use cross-platform Firebase integration, you can also watch the V-Play Engine for mobile applications

 FirebaseDatabase { id: firebaseDb Component.onCompleted: { //use query parameter: firebaseDb.getValue("public/bigqueryobject", { orderByKey: true, //order by key before limiting startAt: "c", //return only keys alphabetically after "c" endAt: "m", //return only keys alphabetically before "m" limitToFirst: 5, //return only first 5 sub-keys }) } } 
0
source share

All Articles