This question already has answers here:
Android: Firebase Search Query not working properly
(2 answers)
Closed 1 year ago.
private void processsearch(String s)
{
FirebaseRecyclerOptions<Calories> options =
new FirebaseRecyclerOptions.Builder<Calories>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Food").orderByChild("Name").startAt(s).endAt(s+"\uf8ff"), Calories.class)
.build();
adapter=new myadapter(options);
adapter.startListening();
recview.setAdapter(adapter);
}
how can I make this search method case insensitive?
I've tried doing
String n="Name";
n.equalsIgnoreCase()
; then replaced the word name in orderbychild with n but it didn't work
I've also tried toLowerCase on n, I can't seem to find the method .equalsIgnoreCase() inside the recylcerOptions that's why i tried to do it on the outside
When you store your strings in your database, you should use a method to make it all lower case, such as string.toLowerCase().
And when you query, you convert your query to lower case as well query.toLowerCase()
there are other factors to consider such as special characters that should be removed/ignored but that is out of the scope of this question.
Related
This question already has answers here:
How to sort Firebase data according to Time Created?
(1 answer)
Firebase - Get Random Child From Database Node
(1 answer)
Closed 1 year ago.
I'm trying to show the user profiles of various users like tinder but I was wondering will the firebase always shows the user who registered first to other new users or if the iteration is random which will guarantee that the result will be random and everyone chances of getting displayed are equal?
If not does someone knows how to make it random in android?
How I'm doing it is
FirebaseDatabase.getInstance().getReference().child("Users").child(OppositeUserSex);
Edit
This is the code where I think sorting wouldn't help because I want it to be random
if(mAuth.getCurrentUser().isEmailVerified())
{
FirebaseDatabase.getInstance().getReference().child("Users").child(UserSex).child(currentUserID).child("isVerified").setValue(true);
}
DatabaseReference oppositeSexDB= FirebaseDatabase.getInstance().getReference().child("Users").child(OppositeUserSex);
System.out.println(OppositeUserSex + "oppositeUser");
oppositeSexDB.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull #NotNull DataSnapshot snapshot, #Nullable #org.jetbrains.annotations.Nullable String previousChildName) {
if(snapshot.exists())
{
if(!snapshot.child("Selection").child("Nah").hasChild(currentUserID)) {
if(snapshot.child("Url").exists() && snapshot.child("isVerified").exists()) { //If user has image only then show them else dont display them && if they are verified
items.add(new ItemModel(snapshot.child("Url").getValue().toString(), snapshot.child("Name").getValue().toString(), "", "", snapshot.getKey()));
}
adapter.notifyDataSetChanged();
}
}
}
Firebase doesn't guarantee that unless you enforce it with the query you make with "orderBy" and also need to specify the direction of sorting / ordering there.
Please have a look here: Order and Limit data with Firebase
So, basically pull the data the way you want it (which would save processing in Android. And if you cannot fully achieve it in query, then you may have to do the additional filtering / ordering in Android code (Java / Kotlin).
Please don't hesitate to ask if you have any more doubts on this. Thanks.
To my experience it shows in the order of when the data was inserted, but I wouldn't rely on that, I always define the fields I will use for sorting and retrieve sorted data based using them.
I bookmarked this video sometime ago, it might have all the answers you need
Sort and Filter Firebase Data
This question already has answers here:
How to add values to Firebase Firestore without overwriting?
(5 answers)
Why is my Cloud Firestore Transaction not merging when using SetOptions.merge()?
(1 answer)
Closed 4 years ago.
I'm new to Firestore and I am having some trouble even though I have gonne through the docs.
I have the following structure:
buses (collection) -> cb-123-1 (document) -> many custom objects
Now I am trying to add data following the docs:
PassengerModel passengerModel = new PassengerModel(
Integer.parseInt(seatNo.getText().toString()),
"selected");
mFirestore.collection("buses").document(mBusUid)
.set(passengerModel, SetOptions.merge());
Now the item is added alright on click however it replaces whatever exists in my document instead of adding to it.
I assume your PassengerModel class has all properties for the passenger. In that case the behavior is expected: Firestore loops through all properties of the class. If no value is present for a property, that property is deleted.
You're more likely looking to patch, which requires a data model that only has the properties that you want to modify. An easier and more common approach is to use a Map for patches, or this even simpler approach:
mFirestore.collection("buses").update(seatNo, Integer.parseInt(seatNo.getText().toString()))
This question already has answers here:
How to remove child nodes in firebase android?
(4 answers)
Closed 5 years ago.
I use following code to add a child and set a value to it in FireBase.
String ref = Constants.Client+"/"+Constants.firebaseProjects+"/"+Constants.ProjectName+"/xyz";
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference(FirebaseRefer);
ref.child("mockChild").push().setValue("")
What can I do to delete the "mockChild" ?
ref.child("mockChild").removeValue();
Code to add child - ref.child("mockChild").push().setValue("Hello");
Code to remove child - ref.child("mockChild").removeValue();
Lets take an example in a user db:
ref.child("Users").child("User1").setvalue("User 1");
ref.child("Users").child("User2").setvalue("User 2");
ref.child("Users").child("User3").setvalue("User 3");
Now if you want to remove a specific user from the database you have to use this code:
ref.child("Users").child("User2").removeValue();
Since firebase is a key value database, it will remove the value from User2 and also the key since the key can't be on it's own. This will remove the entire reference to User2 from your database.
To solve this, please use the following code:
String key = ref.child("mockChild").push().getKey();
ref.child("mockChild").child(key).setValue("yourValue");
ref.child("mockChild").child(key).removeValue(); // This is how you remove it
or you can use:
ref.child("mockChild").child(key).setValue(null);
It's also accepted and means that when you give Firebase a null value for a property/path, you indicate that you want to property or path to be deleted.
If you want to remove the entire child, then just use:
ref.child("mockChild").removeValue();
Note, it will remove everything that mockChild contains.
This question already has answers here:
Good way to replace invalid characters in firebase keys?
(4 answers)
Closed 4 years ago.
I am working on an Android App where I want a Firebase database like
Basically, I want to verify email before creating a user on the app. Whenever a user registers in my app, First it will be checked whether the email address is available in the users database reference. if available, then only he is allowed to register.
But I found that Firebase doesn't allow an email as a child. So How can I do it? Any other suggestion for achieving this in Firebase Database.
Because Firebase does not allow symbols as . in the key, I suggest you encode the email address like this:
name#email.com -> name#email,com
As you probably see, instead of . I have used ,. To achieve this, you can use the following methods:
static String encodeUserEmail(String userEmail) {
return userEmail.replace(".", ",");
}
static String decodeUserEmail(String userEmail) {
return userEmail.replace(",", ".");
}
This question already has answers here:
Firebase complex "contain" queries
(2 answers)
Closed 6 years ago.
Let's say I have this database:
J-DSKFJASKDFJASKDJ:{
username: "Burned"
},
J-KASDJFAKSDJFKDSJ:{
username: "BurnedFirebase"
},
J-DFJKADSJFAKSDJFK:{
username: "FirbaseFilteringIsVeryLackingThereforItSucks"
}
If I use this:
Query q = database.orderByChild("username").equalTo("Burned");
It will actually return only "Burned", although there is a BurnedFirebase in the database. That's because equalTo() doesn't work as a method such as contains().
Is there a method similar to contains()? If not, is there a workaround?
If your search is always at the beginning of the word (you're not searching for ned and expecting Burned to come up), you can use a high unicode character:
query.startAt("Burned").endAt("Burned\uf8ff")
From: https://firebase.google.com/docs/database/admin/retrieve-data#range-queries
...dinosaursRef.orderByKey().startAt("b").endAt("b\uf8ff")...
The \uf8ff character used in the query above is a very high code point
in the Unicode range. Because it is after most regular characters in
Unicode, the query matches all values that start with a b.