firebase: get value with a given key - android

mheadingReference1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapShot : dataSnapshot.getChildren()) {
Log.e("one", "======="+snapShot.child("id").getValue(String.class));
Log.e("one", "======="+snapShot.child("fullname").getValue(String.class));
Log.e("one", "======="+snapShot.child("password").getValue(String.class));
Log.e("one", "======="+snapShot.child("type").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I would like to get the values from firebase and I know the keys of the object. Like "key: id" "value: 123456". The code above displays NULL. How will I be able to get the values, like output "123456" alone. Thanks in advance

Related

How can i get the color with given name firebase

can somebody help me with the valueeventlstener?
i have got this database structure in firebase
Categories
-KvxSVFRZIO3ENylF0id
color: "ff99ffff"
name: "Cars"
-KvxbiS-L6iALA7Os8q4
color: "fffffdd4"
name: "Movies"
now i want to get fffffdd4 in a string when the name Movies is given.
mkatcolordb.child("Categories").orderByChild("name").equalTo(item).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String areaName = dataSnapshot.child("color").getValue(String.class);
StyleableToast.makeText(NewThemaActivity.this, areaName, Toast.LENGTH_LONG, R.style.StyledToast).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
This code doesnt work
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your code needs to handle this. The simplest way to do so for your code:
mkatcolordb.child("Categories").orderByChild("name").equalTo(item).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot colorSnapshot: dataSnapshot.getChildren()) {
String areaName = colorSnapshot.child("color").getValue(String.class);
StyleableToast.makeText(NewThemaActivity.this, areaName, Toast.LENGTH_LONG, R.style.StyledToast).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});

How to handle a json returning data objects with firebase in android

I have an android application which does the following:
com.google.firebase.database.Query query = mUsersRef
.orderByChild(USER_TABLE_EMAIL)
.startAt(searchTerm)
.endAt(searchTerm + FirebaseConstants.SEARCH_ESCAPE);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot d = dataSnapshot;
getView().showSearchResults();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: something?
}
});
I was wondering what the simplest way to convert dataSnapshot into User objects was. Do I need to get returned json and do some form of conversion? or is there a better way?
You can convert your data snapshot into an object as shown below:
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapShot : dataSnapshot.getChildren()){
User user = snapShot.getValue(User.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: something?
}
});

How to retrieve the custom push id from fire base database?

I am having trouble fetching the custom push id which is created while pushing the value.
I want to fetch "4wJIGRCkYrZqFpb401hSmgHeBOI3" from rated_user node. Any help will be really useful. Thanks in advance.
Lets say database reference for rated_users is this RatedUser
valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
String key = postSnapshot.getKey();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
RatedUser.addValueEventListener(valueEventListener);
Use the following code to archive your goal.
private DatabaseReference ref;
ref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://xxxx.firebase.com/rated_users");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
String key = dataSnapshot.getKey();
String value = dataSnapshot.child("key").getValue(String.class);
}catch (Exception e) {
String valws = e.getMessage();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
PS:
I did a quick sample it works for me.
All i did was added a foreach loop:
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Log.e("children" , snapshot.getKey()+"");
}
this gave me the key "4wJIGRCkYrZqFpb401hSmgHeBOI3"

I'm not able to get data from a Firebase database

I'm using Firebase for an Android project and it works well while pushing the data to a server, but when retrieving the data, I get nothing.
here is the structure for my firebase
and here is my code for retrieving the data form Firebase
mRef.child("User").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
counter = dataSnapshot.getChildrenCount();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
post = postSnapshot.getValue(SettersGetters.class);
nicuNames.add(post.getNicuName());
nicuAddresses.add(post.getNicuAddress());
niucCount.add(post.getNicuCount());
nicuPrices.add(post.getNicuCount());
nicuPhones.add(post.getNicuPhone());
nicuUpdate.add(post.getNicuUpdate());
nicuLat.add(post.getNicuLat());
nicuLng.add(post.getNicuLng());
nicuDes.add(post.getNicuDesc());
a++;
Toast.makeText(MapsActivity.this,"jhkjhk", Toast.LENGTH_LONG).show();
System.out.println(nicuNames.get(0));
}
//Toast.makeText(MapsActivity.this,"jhkjhk" + a, Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
While making a Toast to print at least the number of Children, it returns 0 every time
Toast.makeText(MapsActivity.this,a+"",Toast.LENGTH_SHORT).show();
You want to listen to children events of User key, not value events.
Replace
mRef.child("User").addValueEventListener(new ValueEventListener() {
With
mRef.child("User").addChildEventListener(new ChildEventListener() {
Then, implement
onChildAdded(DataSnapshot snapshot, String previousChildName)
onChildChanged(DataSnapshot snapshot, String previousChildName)
And from there, you map a DataSnapshot to a (preferrably better named class than) SettersGetters.class
try this
mRef.child("User").child("Kfx7408plvgugiMR1Se").addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
counter = dataSnapshot.getChildrenCount();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
post = postSnapshot.getValue(SettersGetters.class);
nicuNames.add(post.getNicuName());
nicuAddresses.add(post.getNicuAddress());
niucCount.add(post.getNicuCount());
nicuPrices.add(post.getNicuCount());
nicuPhones.add(post.getNicuPhone());
nicuUpdate.add(post.getNicuUpdate());
nicuLat.add(post.getNicuLat());
nicuLng.add(post.getNicuLng());
nicuDes.add(post.getNicuDesc());
a++;
Toast.makeText(MapsActivity.this,"jhkjhk", Toast.LENGTH_LONG).show();
System.out.println(nicuNames.get(0));
}
//Toast.makeText(MapsActivity.this,"jhkjhk" + a, Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Get last node in Firebase database Android

I want to get item in the last node added in firebase database from my Android. You can see on the image below i'm not sure how to get the specific node, because unique key is created by Firebase. How to reference to auto-created node and child inside? Thanks a lot
The last node
Try this:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query lastQuery = databaseReference.child("mp").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String message = dataSnapshot.child("message").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Handle possible errors.
}
});
Hope this helps!
This might work:
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("mp");
Query query = db.orderByKey().limitToLast(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
Log.d("User key", child.getKey());
Log.d("User val", child.child("message").getValue().toString());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// TODO: Handle errors.
}
});
I prefer to write and retrieve data through objects.
MyObject is POJO.
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
MyObject myObject = data.getValue(MyObject.class);
Log.i(TAG, data.getKey() + " = " + myObject.toString());
}
}
console.log('last messages', messages[messages.length-1]);

Categories

Resources