0

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.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Dean
  • 5
  • 1
  • 5
  • I just gave you an answer but as more I read this the more confuse I get. I you are already setting the TextView with the time, then you are using the time listener for the date picker, so why don't run the query again when the time is selected? Is there any other problem here? – cutiko Apr 01 '18 at 04:34

1 Answers1

0

TimePickers have a time selected listener. Once the time is selected run the query again. The only problem I can see is that the timer selected listeners have hours and minutes arguments that area for less than ten single digits so you have to format that;

//First you have to concatenate the argument you are looking to query

String hours = (h < 10) ? "0"+h : String.valueOf(h);
Stting minutes = (m < 10) ? "0"+h : String.valueOf(m);

//Your harcoded argument is like this "hh:mm"
//so we have to concatenate a String like that
String argument = hours + ":" minutes;

//Then pass the argument to the query
mDatabaseReference.orderByChild("users").equalTo(argument)...

The docs have a very good explanation about the arguments, hours and minute https://developer.android.com/reference/android/widget/TimePicker.OnTimeChangedListener.html

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • I got your point, I run the querry again onTimeSet (without the piece of code you gave me) and it doesn't work, it still ads in my db even though that time was already added. With the code you gave me it gives me an error: Incompatible types. Required: java.lang.String Found: java.io.Serializable & java.lang.Comparable extends java.io.Serializable & java.lang.Comparable> . I'll edit my post and put up that piece of code maybe I'm doing something wrong ... – Dean Apr 01 '18 at 05:55
  • Specifically wrote a comment about the minutes to reduce your issues, but it seems you choose to ignore it – cutiko Apr 01 '18 at 21:59
  • Dude, with the minute or not I still have the error! I edited to show you that I get an error, If I make the minutes, I have two errors now ... – Dean Apr 02 '18 at 16:30
  • @Dean try it again, I think the error was that the int wasn't converted to String – cutiko Apr 03 '18 at 12:44