I'm new to android developing. I tried retrieving data from firebase. which worked . but I'm unable to filter unique identifier from database results .
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myref = database.getReference("user_foods");
myref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("foods",dataSnapshot.getValue().toString());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
results are like
XsgGhY7qUY5Qa3t8 = {food = apple} , ..... etc
I want to remove that unique identifier & just display results as
Apple , Mango ...etc
thank you
I figured the answer. If anyone has same issue use "child event listener "
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myref = database.getReference("user_foods");
myref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("foods",dataSnapshot.getValue().toString());
}
#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) {
}
});
Try this:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myref = database.getReference("user_foods");
myref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
Log.d('foods',dataSnapshot.child('food').getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
adding child() to your reference you can access an specific key in your tree. But as Frank suggests try using a POJO class to retrieve all your data from Firebase
edited: sorry i forgot you had many children, you need to make for (DataSnapshot snap: dataSnapshot.getChildren()) {//code} when your have many children in your node
Related
this is my data and I want to retrieve it by using addValueEventListener but my app unfortunately stop
#Override
public void onStart() {
super.onStart();
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List.clear();
for(DataSnapshot dSnapshot :dataSnapshot.getChildren()){
List<String> td = (ArrayList<String>) dataSnapshot.getValue();
// data data1=dSnapshot.getValue(data.class);
List.add(data1);
}
DataList adapter =new DataList(Fragment1.this.getActivity(),List);
list.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
my app is stopped due to this line of code so tell me any alternative.
//**data data1=dSnapshot.getValue(data.class);
List.add(data1);)
Assuming that your db variable points out to the correct reference.
Instead of using ValueEventListener and looping through each child.
Use ChildEventListener and make sure that the DatabaseReference is pointing to the root where the child resides, so we can iterate through each POJO.
db.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
list.add(dataSnapshot.getValue(YourModel.class)); // Change with your POJO
// Add to your list
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
public void onCancelled(DatabaseError er) { }});
If you want to retrieve Generics in Realtime Database use
Firebase's abstract class GenericTypeIndicator
GenericTypeIndicator<List<Message>> t = new GenericTypeIndicator<List<Message>>() {};
List<Message> messages = snapshot.getValue(t);
use this method
//-----------------// data inside kids----------
private void getDataFrom() {
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference mCustomerDatabase = FirebaseDatabase.getInstance().getReference().child("users").child("kids");
mCustomerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists() && dataSnapshot.getChildrenCount() > 0) {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
if (map.get("kid1ID") != null) {
customer_name = map.get("title").toString();
Log.i("kid1ID",String.valueOf(kid1ID));
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
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) {
}
});
I have data in database:
database in firebase
I want read all child (Belgia, Czechy, Polska...) and display it in Text Field, but AFTER click the button (I don't change data in database).
The button have assigned below function:
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
txt_opis_panstwa.setText(value);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Befrore i write in code:
FirebaseDatabase database;
DatabaseReference myRef;
database = FirebaseDatabase.getInstance();
myRef = database.getReference("kraj");
Unfortunately, when I press the button nothing happens.
I will be grateful for the help
Regards, Marcin
You have to iterate through your children
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for (DataSnapshot country : dataSnapshot.getChildren()) {
txt_opis_panstwa.append(country.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference krajRef = rootRef.child("flagiistolice").child("kraj");
public void f_btn_zaczytaj_dane(View view) {
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String country = ds.getkey();
txt_opis_panstwa.append(country);
Log.d("TAG", country);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
krajRef.addListenerForSingleValueEvent(eventListener);
}
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.
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