Android:关于创建程序预留时间的帮助

时间:2016-10-19 05:17:14

标签: android firebase firebase-realtime-database

我想创建一个可以作为餐馆服务器的应用程序。

我使用timepicker和datepicker在edittext中从用户那里获取时间  我把它改成了一个字符串,但在firebase中没有任何改变。

我想知道如何在firebase中节省时间,并检查是否有其他用户选择时保留该时间。

如何将从datepicker中挑选的日期发送到firebase?

代码是:

public class Room1 extends AppCompatActivity {

boolean FBSetDate = false;
boolean FBSetTime = false;
boolean FBSetTime1 = false;
@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();
    final DatabaseReference dateRefrence = database.getReference("Date");
    final DatabaseReference timeReference = database.getReference("Time");
    final 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);
                    FBSetDate=true;
                    sendDataToFireBase();

                }
            },
                    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);
                    FBSetTime=true;
                    sendDataToFireBase();

                }
            }, 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);
                                           FBSetTime1=true;

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


                               }




                           }
    );



}

private void sendDataToFireBase() {
        if (FBSetTime1 == true && FBSetTime==true&&FBSetDate==true) {
            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());
        }

    }
}

1 个答案:

答案 0 :(得分:0)

在您的代码中,您在数据(日期,时间,时间)更改之前将数据保存到firebase中,因此在firebase中没有保存任何内容。 只要数据/时间值发生这样的变化,您就可以setValue(我直接从帖子中的代码进行编辑):

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();


                               }
                           }
    );



}
}