How to search for more than one value in Firebase
Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: Query based on multiple where clauses in Firebase
In your case however, since you want to search for one value across multiple fields, you will need to perform a separate query for each field.
Search in firebase database for android?
Something like this should do the trick:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users");
ref.orderByChild("name").equalTo("Yash Mehta").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
System.out.println(userSnapshot.getKey());
System.out.println(userSnapshot.child("Name").getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
How to search for a values of Child in firebase Android
In the Web version, they use something called ElasticSearch, you could try to read more here: https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html
But for Android, I think there isn't any functionality to perform a search like that. What I would do is to query all the records then filter them myself:
DatabaseReference databaseReference = mDatabase;
mDatabase.addValueEventListener(new ValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot val : dataSnapshot.getChildren()){
//I am not sure what record are you specifically looking for
//This is if you are getting the Key which is the record ID for your Coupon Object
if(val.getKey().contains("Hotel")){
//Do what you want with the record
}
//This is if your are querying for the hotel child
if(val.child("hotel").getValue(String.class).contains("Hotel")){
//Do what you want with the record
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
}
Search value in firebase realtime on button click
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_student;
mbutton = (Button) findViewById(R.id.search_button);
search = (EditText) findViewById(R.id.input_search);
//Add a TextView here to set the text (optional, depends on how you want to show the retrieved data)
String lastName;
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String studentToSearch = search.getText().toString();
myDbref = FirebaseDatabase.getInstance().getReference().child("Students").child(studentToSearch);
myDbref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
lastName = dataSnapshot.child("last name").getValue().toString();
lastNameTextView.setText(lastName);
}catch (Throwable e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Please try it this way. Here I assumed that you have a child node called "last name" inside the parent node like this Students >> studentToSearch >> last name
.
Just replace anything instead of last name to fetch other information.
How can I search in Firebase using two child values?
You can do it like this. I am assuming that you've split your search keys into two strings i.e. searchKey1 & searchKey2 like below.
String searchKey="abc xyz";
String[] str=searchKey.split(" ");
String searchKey1=str[0];
String searchKey2=str[1];
// OR YOU CAN SPLIT HOWEVER YOU WANT //
databaseReference.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
if(dataSnapshot1.child("uname").exists()&&dataSnapshot1.child("usurname").exists()) {
if(dataSnapshot1.child("uname").getValue().toString().equals(searchKey1)&&dataSnapshot1.child("usurname").getValue().toString().equals(searchKey2)) {
//Do What You Want To Do.
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Searching for EditText value from Firebase
The allDeeJays
node in your JSON doesn't have a string value, so your conditions (like .startAt(t.getText().toString())
) won't return anything.
You're probably looking for:
query = mProfileDatabase.child("allDeeJays")
.orderByValue()
.startAt(t.getText().toString()).endAt(t.getText().toString() + "\uf8ff");
How can I check if a value exists already in a Firebase data class Android
Your approach is wrong.
When you are doing this dataSnapshot.child(busNum).exists()
, it's looking for the busNum
in the key section, where your keys are -kasajdh...
.
So instead what you can do is, get the iterable
, now when you look fordata.child(busNum).exists()
it relates to the value
postRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
if (data.child(busNum).exists()) {
//do ur stuff
} else {
//do something if not exists
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
How to search anywhere in string in Firebase Database - Android
To seach for a string within a String please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference animalsRef = rootRef.child("Animals");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Boolean found;
String search = "Animals";
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String movieName = ds.child("movieName").getValue(String.class);
found = movieName.contains(search);
Log.d("TAG", movieName + " / " + found);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
animalsRef.addListenerForSingleValueEvent(eventListener);
As you see, we query the database for all the movie names and then search for the desired word within the movieName
.
Related Topics
Folder Added on Android Is Not Visible via Usb
Adding Multiple Markers in Google Maps API V2 Android
Error: Mapfragment Cannot Be Cast to Android.Support.V4.App.Fragment
No Peer Certificate' Error in Android 2.3 But Not in 4
Google Drive API Doesn't Play Well with Proguard (Npe)
Yuv to Rgb Conversion by Fragment Shader
Android: Drawing Custom Shapes
Android/Eclipse Panic: Could Not Open
How to Use Mediacodec Without Mediaextractor for H264
Unexpected Namespace Prefix "Xmlns" Found for Tag Linearlayout
Android Class Parcelable with Arraylist
Android Maps API V2 Draw Circle
How Do Cursorloader Automatically Updates the View Even If the App Is Inactive
Android: Onsaveinstancestate Not Being Called from Activity
How to Change the Proguard Mapping File Name in Gradle for Android Project
Android Studio- Program Type Already Present: Com.Google.Android.Gms.Internal.Measurement.Zzwp