从android中写入firebase中的数据

时间:2016-10-22 08:20:59

标签: android firebase firebase-realtime-database

我正在编写一个预留时间的应用程序 我想存储用户预订的日期 在火基地,数据如下所示

Date
  date
       date
            18
              9
               2016
                   Starttime : 8:00
                   Endtime   : 9:00

我希望以这样的方式查看日期,并预订用户ID以在我的后端应用中检索它  就像在这个简单的

 datereserved
        18/9/2016
                  Starttime : 8:00
                  Endtime   : 9:00
                  user      : hisid


                  Starttime :12:00
                  Endtime   :14:00
                  user2      : hisid                     

我预订房间的代码在这里

public class Room1 extends AppCompatActivity {
boolean hasSetTime = false;
boolean hasSetDate = false;
boolean hasSetTime1 = false;
public Map<String, Boolean> stars = new HashMap<>();
public String datetime;
public String Starttime;
public String Endtime;
public Room1(){}
 EditText ft1 ;
 EditText fd1 ;
 EditText e1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.room1);
    final EditText ft1 = (EditText) findViewById(R.id.ft1);
    final EditText fd1 = (EditText) findViewById(R.id.fd1);
    final EditText e1 = (EditText) findViewById(R.id.e1);

   final Button book = (Button) findViewById(R.id.BookBtn);

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



    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);
                    datetime = fd1.getText().toString();
                        hasSetDate=true;
                    Toast.makeText(Room1.this, "choose a date",
                            Toast.LENGTH_SHORT).show();
                }
            },
                    year, month, day);
            datepick.setTitle("select date");
            datepick.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);
                                           Starttime = ft1.getText().toString();
                                           hasSetTime1=true;


                                       }
                                   }, hour, minute1, true
                                   );
                                   timepick.setTitle("select time");
                                   timepick.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);
                    hasSetTime = true;
                    Endtime=e1.getText().toString();

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

        }
    });



   book.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           sendDataToFireBase();
       }
   });


}

 public Room1(String Starttime , String Endtime ) {


     this.Starttime = Starttime;
     this.Endtime = Endtime;
 }
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("EndTime", Endtime);
    result.put("StartTime", Starttime);



    return result;
}


private void TimeReserved() {
// Create new Time
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference  DateReserved = database.getReference("Date");

Room1 post = new Room1(Starttime,Endtime);
Map<String, Object> postValues = post.toMap();

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/Date/"+datetime , postValues);


DateReserved.updateChildren(childUpdates);}
public void sendDataToFireBase() {
    if (hasSetTime == true && hasSetDate==true&&hasSetTime1==true) {
        TimeReserved() ;

    }
    else {
        Toast.makeText(Room1.this, "choose a date correctly",
                Toast.LENGTH_LONG).show();
    }
}
}

0 个答案:

没有答案