How to remove child nodes in firebase android? - android

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();

Related

Not able to use getRef() of Firebase in android

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.

For android firebase how to write the query for finding child by string matching of child of child's value?

Assume this script:
private static DatabaseReference;
mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");
Query firebaseSearchQuery = mUserDatabase.child("service").orderByChild("serviceName").startAt(searchText).endAt(searchText+"\uf8ff");
FirebaseRecyclerAdapter<Workers, WorkerViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Workers, WorkerViewHolder>(
Workers.class,
R.layout.search_result_layout,
WorkerViewHolder.class,
firebaseSearchQuery
) {
#Override
protected void populateViewHolder(WorkerViewHolder viewHolder, Workers model, int position) {
viewHolder.setDetails(model.getName(), model.getEmail());
}
};
What will be the query for finding users email by searching child of child's value (hardware) ?
There is no way to achieve this using this database structure.
First of all between Users node and service node there is another node, named userId, which does not appear in any reference. Between the service node and the child serviceName there is another node, serviceId node, which is the unique key generated by the push() method. I also don't see where are you looping to get those childrens. But these are not the actual problems.
In such cases as yours, you need to use a tehnique named in Firebase, denormalization and for that I recomend you see this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.
So in your case, you need to create another node that will host only the services, so you can simply create a query. Unfortunately, Firebase does not allow you to query after multiple fields, so this is your only choice.

Query nested data from Firebase Real-time database Android

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);

how to filter firebase object using field inside list

I am an android developer, but this is not really important. I accept solution also in other programming language.
I have a collection of "answers" under /root/answers. Here the data is stored using push method, so each child has a unique id. Each child has a field called "user_ref" which is a simple string. I need to populate my list with all answers where a given string match with "user_ref".
So I built my DatabaseReference with:
DatabaseReference r = FirebaseDatabase.getInstance().getReference(ANSWERS_REF)
.startAt("id_123")
.endAt("id_123")
.getRef();
It works but every time this Database references return all answers and not only selected
Try to use equalTo("id_123") instead of startAt() and endAt() (I am assuming that id_123 is a value for user_ref). Namely as follows,
DatabaseReference r = FirebaseDatabase.getInstance().getReference(ANSWERS_REF)
.orderByChild("user_ref")
.equalTo("id_123")
.getRef();
Hope it helps!
The problem is caused by calling getRef() at the end of your statement. That returns the DatabaseReference of the location that you query, so that is all answers.
DatabaseReference allAnswers = FirebaseDatabase.getInstance().getReference(ANSWERS_REF);
Query r = allAnswers.startAt("id_123")
.endAt("id_123");
Since Query is the base class of DatabaseReference, you can also attach listeners to the Query.
r.addChildEventListener(new ChildEventListener() { ...

Android Firebase - Adding Values To Users

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/

Categories

Resources