Home > database >  Store a date&time into Firebase under a User child in Android studio?
Store a date&time into Firebase under a User child in Android studio?

Time:01-07

Here is my Firebase structure:

Firebase structure

I want to store a date and time of day under "Bookings" below the secondName within each User, which will then have the date and time of day selected .

Here is what I have so far which works in a wrong way . It sets a date under a separate child node called " MyBookings "

Essentially what I am doing is clicking a EditText which displays a date and time picker and I need to store that date in Firebase Realtime database.

private void showDateTimeDialog(final EditText date_time_in) {
    final Calendar calendar=Calendar.getInstance();
    DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            calendar.set(Calendar.YEAR,year);
            calendar.set(Calendar.MONTH,month 1);
            calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);

            TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
                    calendar.set(Calendar.MINUTE,minute);

                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm");

                    date_time_in.setText(simpleDateFormat.format(calendar.getTime()));
                    Bookings booking = new Bookings(year,month,dayOfMonth,hourOfDay,minute);
                    rootDatabaseRef.setValue(booking);
                }
            };

            new TimePickerDialog(BookingActivity.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();

        }

    };

    new DatePickerDialog(BookingActivity.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();

}

This is my Firebase reference which I know is wrong but not sure how to write it to be correct.

DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings");

This is my POJO Bookings class.

public int year,month,dayOfMonth,hourOfDay,minute;

public Bookings(){

}

    public Bookings(int year, int month, int dayOfMonth, int hourOfDay, int minute) {
        this.year = year;
        this.month = month;
        this.dayOfMonth = dayOfMonth;
        this.hourOfDay = hourOfDay;
        this.minute = minute;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDayOfMonth() {
        return dayOfMonth;
    }

    public void setDayOfMonth(int dayOfMonth) {
        this.dayOfMonth = dayOfMonth;
    }

    public int getHourOfDay() {
        return hourOfDay;
    }

    public void setHourOfDay(int hourOfDay) {
        this.hourOfDay = hourOfDay;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }
} ```

Current result of the setValue() method.

MyBookings
dayOfMonth: 
6
hourOfDay: 
13
minute: 
49
month: 
0
year: 
2022

How do I store the date time picked correctly under a User(only for the current user logged in) in Firebase?

CodePudding user response:

Change this:

DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings");

to this:

DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username).child("Bookings");

Where username is a String containing the user name of the current user.

Improved: Keep a counter for the bookings.. let's say nBooks Now, make the reference like this ...child(username).child("Booking" nBooks)

CodePudding user response:

You are initialising rootDatabseRef with FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username);

But if you want to add it under separate node of you UID, you need to initialise in this way

String username = ""/*This where you set you currently logged user's did*/;
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username).child(UUID.randomUUID().toString());
  •  Tags:  
  • Related