I've done this push:
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mDatabaseReference.child(TipoAnuncio).push().setValue(fichaPet);
(Where fichaPet is a ad's class which is pushed in other object from firebase database)
My BBDD:
Users Firebase Database
I wanna get push's ID to write inside Ads from Users of FirebaseDatabase, and do something like this:
mDatabaseReference.child("Usuarios").child(userId).child("Ads").push().setValue(mDatabaseReference.child("Usuarios").child(userId).child("Ads").push().getKey());
but it doesn't get previously ID from previously ad published.
Thanks!!! :)
To solve this, please use the following code:
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference tipoAnuncioRef = mDatabaseReference.child(TipoAnuncio);
String key = tipoAnuncioRef.push().getKey(); //This is the value of your key
tipoAnuncioRef.child(key).setValue(fichaPet);
If you are using the push() methid again, a new id will be generated and there is no need for that.
Related
I am facing issues with my RecyclerView in displaying my app's candidates when I include the added map field in my query. I also have created an index for this query but still, nothing comes out of my RecyclerView.
This is the UPDATED query code. If there is something wrong with the coding, please do notify me.
FirebaseUser user = mAuth.getCurrentUser();
String voterID = user.getUid();
Query query = notebookRef.whereEqualTo("registrationStatus", "Accepted").whereEqualTo( voterID, false).orderBy("candidateFullName", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<VoterCandidateItem> options = new FirestoreRecyclerOptions.Builder<VoterCandidateItem>()
.setQuery(query, VoterCandidateItem.class)
.build();
adapter = new VoterCandidateAdapter(options);
recyclerView = findViewById(R.id.candidateList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(null);
This is the UPDATED screenshot of Firestore DB:
And this is the UPDATED screenshot of the index:
The following query:
Query query = notebookRef
.whereEqualTo("registrationStatus", "Accepted")
.whereEqualTo(voterID, false)
.orderBy("candidateFullName", Query.Direction.ASCENDING);
Requires an index, otherwise, it will not work. If you are using Android Studio, you'll find in the logcat an error message that looks like this:
FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/...
The required query can be created either directly in the Firebase Console, or by clicking on that link. If the click doesn't work, simply copy and paste the URL into a web browser and your index will be created automatically for you.
If you choose the console, remember to set:
Collection: Candidates
Fields: registrationStatus ascending and voterID: ascending
I'm currently working on a project and I need to be able to delete from a database I've already created. I created it using the .push() to the database hence a unique key is created and thus needed for delete operations.
I tried using the answer from Frank Van Puffelen here How to delete from firebase realtime database?, but I ran into a bug where if two nodes have the same title they'll be deleted.enter image description here
The image shows how my Firebase database looks:
A bit of assistance or direction to an answer would go a long way. Thanks
If you want to delete a single element from your database, you need to know the path to that element, which includes also that pushed key. Your reference should look like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference keyRef = rootRef.child("-KlSNx87aYigsH3lLp0D");
keyRef.removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "Element removed successfully!");
}
}
});
Otherwise, you can use a query that looks exactly like this:
Query idQuery = rootRef.orderByChild("id").equalTo(1);
In this case, Frank Van Puffelen's answer from the following post:
How to delete from firebase realtime database?
Will work perfectly fine.
I need to retrieve some data on Firebase realtime database based on the values of a child.
For example, here I need to show only the child(notification) where accepted==true.
I pass my values on RecyclerView with a reference without creating a list.
To get all notifications "WHERE" accepted property holds the value of true, you need to use a query. In code, looks like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference notificationsRef = rootRef.child("Notifications");
Query query = notificationsRef.orderByChild("accepted").equalTo(true);
query.addListenerForSingleValueEvent(/* ... */);
Have you tried this?
https://firebase.google.com/docs/database/android/lists-of-data#filtering_data
You can use equalTo() method to filter data.
I want to see if a value is in my firebase database, the user will provide the value thereafter I want to say if the value is present then move to the next activity.
Data in firebase is retrieved by adding a asynchronous listener to a database reference. So you have to change the way you are used to retrieve data from other data sources like MySQL...
Thus, define the reference to the part of firebase database where the user is going to insert its data and attach listener to it. In the listener write your code to go to the next activity.
For example:
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/NodeOfTheUser");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//your code to go to the next activity
}
});
For more info: firebase documentation.
I have the following Firebase database structure, and I would like to retrieve the entire object that has a host of "Mike 22".
This is not in a ValueEventListener event, so I don't have access to the DataSnapshot. How can I retrieve the object based on that query? I want to do it in an SQL style where you'd type SELECT object FROM objects WHERE host = "Mike 22". Is there any way to do this?
Currently I have the reference to the database as seen below:
mDatabase = FirebaseDatabase.getInstance().getReference().child("objects");
This is covered in Sorting and filtering data in the Android Guide. Use orderByChild and equalTo:
mDatabase = FirebaseDatabase.getInstance().getReference().child("objects").orderByChild("host").equalTo("Mike 22");