Omar Elshirbini Omar Elshirbini - 1 month ago 7
Android Question

Android:Help about creating a program the reserve time

I want to create an app that works as a reserver for a restaurant.

I used timepicker and datepicker to get the time from the user in edittext
and I changed that to a string but in firebase there is nothing changed.

I want to know how to save that time in firebase and check if that time reserved if other user selected it.

How I can send date that picked from datepicker to firebase?

The code is:

public class Room1 extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.room1);


final Calendar c = Calendar.getInstance();
final int year = c.get(Calendar.YEAR);
final int month = c.get(Calendar.MONTH);
final int day = c.get(Calendar.DAY_OF_MONTH);
final int hour = c.get(Calendar.HOUR_OF_DAY);
final int minute1 = c.get(Calendar.MINUTE);
final Calendar v = Calendar.getInstance();
final int hour2 = v.get(Calendar.HOUR_OF_DAY);
final int minute2 = v.get(Calendar.MINUTE);

final String date;
final EditText ft1 = (EditText) findViewById(R.id.ft1);
final EditText fd1 = (EditText) findViewById(R.id.fd1);
final EditText e1 = (EditText) findViewById(R.id.e1);

fd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datepick = new DatePickerDialog(Room1.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
fd1.setText(dayOfMonth+"/"+month+"/"+year);

SimpleDateFormat dateFormatter ;
Date d ;
String entered_dob ;



d = new Date(year, month, day);
dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
entered_dob = dateFormatter.format(fd1.getText());
}
},
year, month, day);
datepick.setTitle("select date");
datepick.show();

}
});

e1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { TimePickerDialog timepickend = new TimePickerDialog(Room1.this, new TimePickerDialog.OnTimeSetListener() {


@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
e1.setText(hourOfDay + ":" + minute);


}
}, hour2, minute2, true
);
timepickend.setTitle("select time");
timepickend.show();

}
});

ft1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimePickerDialog timepick = new TimePickerDialog(Room1.this, new TimePickerDialog.OnTimeSetListener() {


@Override
public void onTimeSet(TimePicker view, int hourOfDay2, int minute2) {
ft1.setText(hourOfDay2 + ":" + minute2);


}
}, hour, minute1, true
);
timepick.setTitle("select time");
timepick.show();


}
}
);


FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Date");
myRef.setValue(ft1.getText().toString());
DatabaseReference myRef2 = database.getReference("Time");
myRef2.setValue(e1.getText().toString());
DatabaseReference myRef3 = database.getReference("Timereserved");
myRef2.setValue(fd1.getText().toString());

}


}

Answer

in your code, you save data into firebase before the data (date, time, timereserved) change, so nothing saved in firebase. you can setValue whenever the data/time value changed like this (I edit directly from the code in your post):

public class Room1 extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.room1);


    final Calendar c = Calendar.getInstance();
    final int year = c.get(Calendar.YEAR);
    final int month = c.get(Calendar.MONTH);
    final int day = c.get(Calendar.DAY_OF_MONTH);
    final int hour = c.get(Calendar.HOUR_OF_DAY);
    final int minute1 = c.get(Calendar.MINUTE);
    final Calendar v = Calendar.getInstance();
    final int hour2 = v.get(Calendar.HOUR_OF_DAY);
    final int minute2 = v.get(Calendar.MINUTE);

    final String date;
    final EditText ft1 = (EditText) findViewById(R.id.ft1);
    final EditText fd1 = (EditText) findViewById(R.id.fd1);
    final EditText e1 = (EditText) findViewById(R.id.e1);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference dateRefrence = database.getReference("Date");
    DatabaseReference timeReference = database.getReference("Time");
    DatabaseReference timeReservedReference = database.getReference("Timereserved");

    fd1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatePickerDialog datepick = new DatePickerDialog(Room1.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    fd1.setText(dayOfMonth+"/"+month+"/"+year);
                    dateRefrence.setValue(dayOfMonth+"/"+month+"/"+year);

                    SimpleDateFormat dateFormatter ;
                    Date d ;
                    String entered_dob ;



                    d = new Date(year, month, day);
                    dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
                    entered_dob = dateFormatter.format(fd1.getText());
                }
            },
                    year, month, day);
            datepick.setTitle("select date");
            datepick.show();

        }
    });

    e1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {      
            TimePickerDialog timepickend = new TimePickerDialog(Room1.this, new TimePickerDialog.OnTimeSetListener() {


            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                e1.setText(hourOfDay + ":" + minute);
                timeReference.setValue(hourOfDay + ":" + minute)


            }
        }, hour2, minute2, true
        );
            timepickend.setTitle("select time");
            timepickend.show();

        }
    });

    ft1.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View v) {
                                   TimePickerDialog timepick = new TimePickerDialog(Room1.this, new TimePickerDialog.OnTimeSetListener() {


                                       @Override
                                       public void onTimeSet(TimePicker view, int hourOfDay2, int minute2) {
                                           ft1.setText(hourOfDay2 + ":" + minute2);
                                            timeReservedReference.setValue(hourOfDay2 + ":" + minute2);

                                       }
                                   }, hour, minute1, true
                                   );
                                   timepick.setTitle("select time");
                                   timepick.show();


                               }
                           }
    );



}
}