i am trying to get the key that i used to push information to my table. Is there any other way to JUST get the key itself.
i am trying to get the red marked key
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
if(snapshot.exists()){
Toast.makeText(getActivity(), dataSnapshot.getValue().toString() , Toast.LENGTH_LONG).show();// try to get ID
}
}}
this is what i keep on getting
is there any other possible way on how i could get the red mark, directly?
DatabaseReference bdRef = FirebaseDatabase.getInstance().getReference()
.child("Lobby").orderBy("lobbyname").equalTo("admin lobby");
bdRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
Toast.makeText(getActivity(), child.getKey().toString() , Toast.LENGTH_LONG).show();// try to get ID
}
}
Related
My Database Structure:
Hi,I'm currently working on Android Studio project. How do I retrieve a list of data in firebase with random generate key? For example, I need to retrieve the institute name, address and phone. I'm having the random generated id, how do I reference it? Thanks.
To retrieve the name, address and phone try the following:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String name = ds.child("instituteName").getValue(String.class);
String phone = ds.child("institutePhone").getValue(String.class);
String address = ds.child("instituteAddress").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
First add a reference to the root node then add a for loop to iterate inside the random if and retrieve the data.
String userID = selectedCharacter.getUserID();
String charID = selectedCharacter.getCharID();
Character editedCharacter = new Character(userID, charID, name, hitPoints, armorClass, level, experience, gold);
databaseRef
.orderByChild("charID")
.equalTo(selectedCharacter.getCharID())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Translate the character to a map of its data
Map<String,Object> updates = editedCharacter.toMap();
// Update ONLY the node with charID = editedCharacter.getCharID()
databaseRef.updateChildren(updates);
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
So I'm trying to update a character's stats in my firebase database. As you can see:
here
the code I'm using is actually putting the update in character's root instead. What am I doing wrong here? I'm unsure of how to find the node with the key as I'm not storing the key anywhere.
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.
So your code will need to handle the fact that the snapshot is a list. In the case of Android that means that you loop over snapshot.getChildren():
databaseRef
.orderByChild("charID")
.equalTo(selectedCharacter.getCharID())
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
// Translate the character to a map of its data
Map<String,Object> updates = editedCharacter.toMap();
// Update ONLY the node with charID = editedCharacter.getCharID()
child.getRef().updateChildren(updates);
}
}
Instead of databaseRef.updateChildren(updates);
Could you try
databaseRef.child(dataSnapshot.getKey()).setValue(updates)
Then see if it works.
I am new to firebase and nosql database
I am trying to retrieve some specific data from a Firebase. I have a node Drugs and Assaults and that node has many unique identifier as node which has so more data into it. I want to retrieve lat/lng from each node.
Please have a look at this.Firebase database image
To get the lat/lng, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference assaultRef = rootRef.child("Assault");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String latlng = ds.child("liststring").child("2").getValue(String.class);
Log.d("TAG", latlng);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
assaultRef.addListenerForSingleValueEvent(eventListener);
You output will be:
lat/lng: (20.330198..., 77.809580...)
lat/lng: (30.786742..., 62.664079...)
Is it possible to fetch a child from an object without the entire parent in Firebase
For example, a customer registry, where I need all the "name" fields, but I do not have the user "uid". ...
Alex's answer will work (with my comment for good measure).
But you should realize that this downloads all data of all users. There is no way in the Firebase Database to download just one property of each node. So if you want an efficient way to download just the list of names, you should keep precisely that in the database: a list of names.
usernames
uid1: "Fernando"
uid2: "Alex"
That way you can read just the list of names with:
DatabaseReference usernamesRef = FirebaseDatabase.getInstance().getReference().child("usernames");
usernamesRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String uid = childSnapshot.getKey();
String name = childSnapshot.getValue(String.class);
}
Yes it's possible. Please use this code:
DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("usuarios");
usersRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String uId = (String) ds.getKey();
String nome = ds.getChild("nome").getValue(String.class);
list.add(nome);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
You first iterate to get those uid's and after that use them in the DatabaseReference.
Hope it helps.
What should be my query be if I want to get the list of all records which gender = 0 and userType = 1? I'm quite new to firebase and I can't find any sources that teaches compound some queries.
first of all create a node name userList and add instances of user with key 0,1,2,3 and so on like in screen shot in question. It becomes an array of userList
List<User> userList = new ArrayList<>();
Firebase ref = new Firebase(FIREBASE_URL);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
<User> user = postSnapshot.getValue(<User>.class);
userList .add(user);
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("The read failed: " ,firebaseError.getMessage());
}
});
And you can also use Firebase RecyclerAdapter like this
Firebase is no SQL. it doesn't have where clause. You can use orderBy,startAt,endAt function to achieve filtering . See this and this.
Filter most on the server, do the rest on the client
orderBy('gender')
.startAt('0').endAt('0')
and in onDataChange
for (DataSnapshot postSnapshot: snapshot.getChildren())
{
<User> user = postSnapshot.getValue(<User>.class);
if(user.getUserType == 1)
userList .add(user);
}