I'm trying to read all the values from my firebase database. My child values are randomly generated using
.push.getkey()
My database structure looks as below:
database structure
Example: child -MFo2XLRF34Pez_xeVAT contains a Jobs object.
How can I get all the values of randomly generated child nodes ? Since i can't pass the values to datasource.child()since I don't know the name of the child.
I just want to save all the Jobs objects into an ArrayList but I can't access them.
Make a Job class with getter and setter for the information you have at that node then to get the data you do this:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Jobs");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot:snapshot.getChildren())
{
Jobs jobs = dataSnapshot.getValue(Jobs.class);
jobsList.add(jobs);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
the problem was in my databasereference. Instead of assigning databasereference = FirebaseDatabase.getInstance().getReference("Jobs") i was using FirebaseDatabase.getInstance().getReference() which was pointing to the root node. After that dataSnapshot.getValue() worked like a charm.
Related
I have a simple database structure like so:
I need to remove the value earphone by using its key which is -M-4XBDqv-ve396r5EHm.
I have a button for deleting that value,
and I want to use this line of code to remove it:
mReferenceForListA.child("List A").child("Items").child(key).removeValue().
But how do I get the key for earphone?
I know I can just write
mReferenceForListA.child("List A").child("Items").child(-M-4XBDqv-ve396r5EHm).removeValue()
to delete earphone straight away,
but what if I need to delete other items in the list?
I'll need to pass a specific key into a variable called "key" to delete a specific value.
So, again, how could I get a specific key for the value that I want to delete in my current JSON structure?
This is what I tried:
Reading through related posts for days in stackoverflow and I came down to writing the following codes, but I don't think I got the hang of this thing yet and seriously need help. The following codes always delete the whole "List A" node, but in this case I just need to delete one specified value and keep the others intact in the node.
public static DatabaseReference mRootReference = FirebaseDatabase.getInstance().getReference();
public static DatabaseReference mReferenceForListA = mRootReference.child("List A");
deleteButton= findViewById(R.id.delete_button)
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mReferenceForListA.child("Items").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot :dataSnapshot.getChildren()) {
String key = postsnapshot.getKey();
mReferenceForListA.child("Items").child(key).removeValue();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
To delete a node you need to know its complete path. If you only know a value of (a property of) the node, you can run a query to find all nodes that match that value.
Something like:
public static DatabaseReference mRootReference = FirebaseDatabase.getInstance().getReference();
public static DatabaseReference mReferenceForListA = mRootReference.child("List A");
Query query = mReferenceForListA.child("Items").orderByValue().equalTo("earphone");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
snapshot.getRef().removeValue();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
The main changes:
The above uses a query on orderByValue() on the Items child. This means Firebase searches all child nodes directly under List A/Items and returns the ones whose value matches earphones.
It also uses addListenerForSingleValueEvent, so that the delete only runs once, instead of continuously.
It uses snapshot.getRef().removeValue() for a much shorter way to remove the node.
It implements onCancelled, since you should never ignore errors.
You should iterate through the DataSnapshot as you have done, but you also need to supply it with a condition to delete the entry with a specific value.
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
if(postsnapshot.child(postsnapshot.getKey()).getValue(String.class)).equals("earphone"){
mReferenceForListA.child("List A").child("Items").child(postsnapshot.getkey()).removeValue();
}
}
Let me know if this works as I am trying to write this from memory.
I have a firebase database I am using with Android.
To Retrieve and show data from Firebase, I am using FirebaseRecyclerAdapter and results are displayed to CardView.
Now I am getting all listings from my required table to CardView. Every Node has an automatically generated value as shown in the image below.
How do I retrieve this value in Android when I click on respective CardView. I'll need this value for further database operations.
Now, I tried some of the methods posted here as well as online forums but nothing seems to work. I am not asking for complete code, just how to do it? Some reference code would be great.
Regards
To get the node id try the following:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Projects");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);
First, add a reference to node Projects then loop inside of it and retrieve the id using getKey().
I can't figure out how to retrieve my data without variable under the push key, so please help.
reference = FirebaseDatabase.getInstance().getReference("FireAlarm");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot keySnapshot : dataSnapshot.getChildren()){
dataBaru = keySnapshot.getKey();
data.setText(dataBaru);
}
}
When you are using the following line of code:
dataBaru = keySnapshot.getKey();
You are getting only those pushed keys. If you want to get the values corresponding to the keys, please use the following line of code:
dataBaru = keySnapshot.getValue(String.class);
One more thing, when looping through a DataSnapshot object using getChildren() method, you're most likely getting more than one result so you can simply set it to a TextView. So probably you should use a ListView for that or even better a RecyclerView.
I am using the "push()" function to store objects into my firebase database and since it stores data with unique keys I can't change the stored objects values using what's mentionned in the documentation:
myRef.child("child name").setValue(model);
because I simply don't know the key. I wan wondering is there an other way to modify the data of firebase database.
I want to add a new child called "conversations" to the users and programmatically add childs to him .
and this is the databse structure:
If you have the following database:
Users
pushId
name : john
age : 100
In case you dont know the pushId, you can do the following:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
String name = ds.child("name").getValue().toString();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The above will get you the name and the key using getKey()
I am attempting to delete a value from a node in Firebase.
It seems that the onDataChange is not being reached.
I tried looking up the docs and articles and used some of the code that I saw referenced in multiple places(e.g. Remove Specific value From Firebase Database). Log statements inside onDataChange are not being called but the Log statements before it are. The scope of email is fine as can be seen by the log statement
I/emailRemove: 111#aol.com
I/removeEmail: Inside Method
In this example I would like to delete whatever area has the email "111#aol.com" :
private void removeEmail() {
Log.i("emailRemove", email);
Log.i("removeEmail","Inside Method");
Query emailQuery = mDatabase.child("emailAddress").equalTo(email);
emailQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot emailSnapshot: dataSnapshot.getChildren()) {
Log.i("snapshot", emailSnapshot.toString());
Log.i("snapshotRef", emailSnapshot.getRef().toString());
emailSnapshot.getRef().removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("TAG", "onCancelled", databaseError.toException());
}
});
}
Since you want to filter by a child property, you should use orderByChild(...):
Query emailQuery = mDatabase.orderByChild("emailAddress").equalTo(email);
emailQuery.addListenerForSingleValueEvent(new ValueEventListener()
You'll also want to make sure you run this query against RC9..., since that is the only node where the child nodes have an emailAddress property.