So I've been wondering for a couple of days now but don't seem to get the right answer. I have a Firebase database and I want to search for data in one child. Now, I've seen manny good examples here. I know how to search for particular data:
mDatabaseReference.orderByChild("Time").equalTo("10:00").
addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()){
System.out.println(userSnapshot.getKey());
System.out.println(userSnapshot.child("time").getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
In the example above I can search in my child database for "10:00"
. It works great, I have this example from here "Search in firebase database for android?".
The thing is, I have a time picker. The time picker data is stored in a textView. Like this:
pickTimeTextView = findViewById(R.id.timePickTextView);
On the on Onclick for the time picker:
pickTimeTextView.setText("");
And on the OnTimeSet:
pickTimeTextView.setText(String.format(getString(R.string.hour_selected) + "%02d:%02d", hourOfDay, minute));
My question is if I have a chance to search my firebase database with the content of the textView? The String in the textView will change with the time selected by me in the time picker. Do you guys think I can do this with firebase? If not, can you point me in the right direction? Thank you in advance!
Later Edit:
I put the query in OnTimeSetListner (it doesn't work without the format):
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
pickTimeTextView.setText(String.format(getString(R.string.hour_selected) + "%02d:%02d", hourOfDay, minute));
String hours = (hourOfDay < 10) ? "0" + hourOfDay : hourOfDay;
mDatabaseReference.orderByChild("users").equalTo(hourOfDay + minute).
addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()){
System.out.println(userSnapshot.getKey());
System.out.println(userSnapshot.child("users").getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
And I tried to format the hours and minutes arguments as cutiko instructed, but when I do that I get this error:
Incompatible types. Required: java.lang.String Found: java.io.Serializable & java.lang.Comparable
I read again what I wrote and I don't thinl I explained the problem the best way I could. I want to search in the firebase db to see if the selected (time by the user) is already stored. And if it is, send the user an error. I use a time picker to select the time and the time picker is restricted, only shows time between 8am and 11am.