How to read only one value from Firebase Database in Android Studio? - android

This is my database in Firebase Database:
How can I read the value of only a1 from my Firebase Database in my Android app and store it in a String variable ra1? Example Java code using the given database would be really helpful.
Thank You.

In your case, you just want to retrieve that String ONCE, and not listen for any changes.
So, what you do:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot){
String value = dataSnapshot.child("a1").getValue(String.class); //This is a1
}
});

Initiliaze root and add childeventlistener:
DatabaseReference root = FirebaseDatabase.getInstance().getReference().child("morpion_c76af");
root.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(dataSnapshot.getKey().equals("a1")) {
String a = (String) dataSnapshot.getValue()
}
}
#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) {
}
});
Hope this helps.

Related

how can i order recyclerview items in firebase by replay timestamps

how can I order my recyclerview items like this
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
productQuery = mDatabaseReference.child("Blog").orderByChild("replaydate");
"replaydate" is located in Replay>psotkey>replaydate I want order blog items by repaydate, is this possiple?
If you want to get the messages by timestamp order you can use
database.orderByChild("replydate").limitToLast(10)
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Model newModel = dataSnapshot.getValue(Model.class);
//Model is you model which you are using for showing data in recyclerview
arraylist.add(newModel);
adapter.notifyDatasetChanged();
}
#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) {
}
});

Get the values of a child

root
-messages
--LIB38128eG7v
-receiverID: "yoon"
-senderID: "kim"
-text: "good day"
--LIB36334eG9e
-receiverID: "yoon"
-senderID: "kim"
-text: "good night"
this is my firebase database.
and there is a class named Chat, which has receiverID, senderID, text.
I can get all the data of messages by
ref.child("messages").addChildEventListener(new ChildEventListener(){
public void onChildAdded(Datasnapshot dataSnapshot, String s){
Chat chat = dataSnapshot.getValue(Chat.class);
mChat.add(chat); //mChat is an array of Chat
....
}
But I want to get the data of LIB38128eG7v, not the all of the messages. In this case, how can I get one of the child's child values?
Is there any way not using listener?
There is no way to get the data from a Firebase database without using a listener. Everything in Firebase is about listeners. To get the data under -LIB38128eG7v, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference messageIdRef = rootRef.child("messages").child("-LIB38128eG7v");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Chat chat = dataSnapshot.getValue(Chat.class);
Log.d("TAG", chat.getText());
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
messageIdRef.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be: good day.
I think this way it will work
DatabaseReference rootRef =
FirebaseDatabase.getInstance().getReference().child("messages").child("-
LIB38128eG7v");
rootRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//your code
}
#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) {
}
});

Retrieve and Show Node in Listview

Is there anyone here could help on how to retrieve those class section inside the black box and show it in listview and if i add more class section it will add automatically in my listview. Thanks
So, if your database structure is like this..
teacher{
VRdn7....f1{
name:"name"
.....
class{
key1{
--> className:"c1" }
key2{
--> className:"c2" }
key3{
--> className:"c3" }
}
}
}
And if you want values of className then,
DatabaseReference db = FirebaseDatabase.getInstance()
.getReference("teacher").child("VRdn7......f1");
db.child("class").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String className = dataSnapshot.child("className").getValue().toString();
//This will get all values of className inside "teacher\VRdn7....f1\class\***\"
// Now create adapter for listview, and then add values in a list....
}
#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) { }
});

How to add real-time listener to firebase query

I've created a database to store my logs and get real-time update of my log entries for the last 1 hour
I've tried adding a real-time listener to my firebase query. Both attempt triggered once (upon start-up), but didn't trigger when a new log was added while the app is already running
Here are my code
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("logs");
long MillisInHour = 60*60*1000;//one hour
Here is the query
Query query = myRef.orderByChild("timeStamp").startAt(System.currentTimeMillis()).endAt(System.currentTimeMillis() - MillisInHour);
I've tried (Attempt #1)
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
Also (Attempt #2)
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String 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) {}
});
Found it. I should have used
Query query = myRef.orderByChild("timeStamp").startAt(System.currentTimeMillis() - MillisInHour)
instead.

Firebase change data from server, listen on Android

I'm playing with Firebase. Updating data in server with node.js:
var db = firebase.database()
var ref = db.ref("game")
ref.update({
"state": "state",
"counter": counter
})
The data in Firebase console updated without problem but in Android, I can't retrieve any change. I want to listen "game" node's changes but no luck.
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("game");
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log("1");
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log("2");
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log("3");
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log("4");
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log("5");
}
});
There is no log in console.
Also, with ValueEventListener I'm still not seeing any change or error:
DatabaseReference ref = database.child("game");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log("1");
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log("2");
}
});
I'm clearly doing something wrong. How can I listen the data changes in FireBase?
I 'solved' the issue by re-logging by user. I guess this is a bug.
Relevant: Firebase single value event listener doesn't return

Categories

Resources