I am making an Android app with Firebase and I want to add sections to each FirebaseUser (like wins and losses) but I don't think you can add to those objects. Should I just use the Realtime Database to do this or is there a better way?
Thanks in advance!
DatabaseReference ref1= FirebaseDatabase.getInstance().getReference();
DatabaseReference ref2,ref3;
ref2= ref1.child(user);//user is the name of user
ref2.child("Wins").setValue(""what he won);
ref2.child("Loose").setValue(""what he Lose);
Here I created a child to root node with user name, added sub children(Win & Lose) to user and then I set values to these children.
And you can retrieve that data too based on your query.
Refer following link once.
https://firebase.google.com/docs/database/android/start/
Related
I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.
I have the structure in the image below and I want to get the whole data from the node "aaa1234" querying by "id_Usuario". How can I do that query?
I tried:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
Query query = root.orderByChild("id_Usuario").equalTo(2);
Just to be more clear, the "aaa1234" is a custom key, so it will be different in the next node.
I see two mistakes.
The first one is a typo, which I already marked in my comment. You're storing the user ID as a number, so shouldn't have quotes around the value in equalTo. So: .equalTo(2)
The second mistake is in the way you try to query:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
This will create a reference to the non-existing child Eventos/participantes. What you want to do instead is query Eventos, but then against property participantes/id_Usuario. So
DatabaseReference root = database.getReference().child("Eventos");
Query query = root.orderByChild("participantes/id_Usuario").equalTo(2);
I have a simple question about firebase
I'm creating an app which adding some data to the firebase,
when the user clicks on the button, some data should be added to firebase
How can I do this by coding ?
First, you need to create a model with (firstname,lastname,phone), and then get reference to the database as shown below.
MainData mainData =new MainData("ABC","XYZ","789798");
DatabaseReference mRefrence = mDatabase.child("users").getRef().push();
mRefrence.setValue(mainData);
By using push with a reference you can add values to the firebase database.
I have the following structure saved in my database:
What I want is I want the databaseReference to recognise the entire node using only the unique imageUIDh and display the content of all fields like postedAtTime, postedOnDate etc.
Is this possible in Firebase? Is there some alternative to achieve what I want?
Please let me know.
You can use Query
ref = FirebaseDatabase.getInstance().getReference();
ref.orderByChild("imageUIDh").equalTo(imageUIDh).addValueEventListener(valueEventListener);
Hope this helps :)
I have a number of child nodes in my firebase db and I want to delete only one child node.
Firebase firebase=new Firebase("..address..");
firebase.push().setValue(classObj);
//here classObj is a class object which has a getter and setter for an integer id
Now that I have pushed multiple objects I want to delete only one based on the id in the classObj
To remove data:
firebase.child(id).removeValue();
You might do well to have a look at the Firebase documentation for Android btw, which covers this and many more topics.
If you are using DatabaseReference for firebase
DatabaseReference dbNode = FirebaseDatabase.getInstance().getReference().getRoot().child("Node");
Here Node represents the child which you wish to delete
dbNode.setValue(null);
If you are using a dataSnapshot
i.e., while you are working on some data change events
dataSnapshot.getRef().setValue(null);
To remove a Node or child node
private FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference("root_node_name")
.child("child_node_name")
.removeValue();
To remove Sub-child node
database.getReference("root_node_name")
.child("child_node_name")
.child("sub_child_node_name")
.removeValue();
You need to run this code:
Firebase firebase=new Firebase(URL);
firebase.child(id).removeValue();