在 Android studio 中将日期和时间存储到用户子项下的 Firebase 中?
Store a date&time into Firebase under a User child in Android studio?
这是我的 Firebase 结构:
我想在每个用户的 secondName 下方的“预订”下存储日期和时间,然后将选择日期和时间。
这是我目前所用的错误方式。它在名为“MyBookings”的单独子节点下设置日期
基本上我所做的是单击显示日期和时间选择器的 EditText,我需要将该日期存储在 Firebase 实时数据库中。
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();
}
这是我的 Firebase 参考资料,我知道它是错误的,但不确定如何正确编写它。
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings");
这是我的 POJO 预订 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;
}
} ```
setValue() 方法的当前结果。
MyBookings
dayOfMonth:
6
hourOfDay:
13
minute:
49
month:
0
year:
2022
如何在 Firebase 中存储用户(仅适用于当前登录的用户)正确选择的日期和时间?
改变这个:
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings");
对此:
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username).child("Bookings");
其中 用户名 是包含当前用户的用户名的字符串。
改进:保留一个预订柜台..比方说 nBooks
现在,像这样进行引用 ...child(username).child("Booking"+nBooks)
您正在用 FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username);
初始化 rootDatabseRef
但是如果你想把它添加到你的UID的单独节点下,你需要这样初始化
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());
这是我的 Firebase 结构:
我想在每个用户的 secondName 下方的“预订”下存储日期和时间,然后将选择日期和时间。
这是我目前所用的错误方式。它在名为“MyBookings”的单独子节点下设置日期
基本上我所做的是单击显示日期和时间选择器的 EditText,我需要将该日期存储在 Firebase 实时数据库中。
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();
}
这是我的 Firebase 参考资料,我知道它是错误的,但不确定如何正确编写它。
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings");
这是我的 POJO 预订 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;
}
} ```
setValue() 方法的当前结果。
MyBookings
dayOfMonth:
6
hourOfDay:
13
minute:
49
month:
0
year:
2022
如何在 Firebase 中存储用户(仅适用于当前登录的用户)正确选择的日期和时间?
改变这个:
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings");
对此:
DatabaseReference rootDatabaseRef;
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username).child("Bookings");
其中 用户名 是包含当前用户的用户名的字符串。
改进:保留一个预订柜台..比方说 nBooks 现在,像这样进行引用 ...child(username).child("Booking"+nBooks)
您正在用 FirebaseDatabase.getInstance().getReference().child("MyBookings").child(username);
rootDatabseRef
但是如果你想把它添加到你的UID的单独节点下,你需要这样初始化
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());