Here's my problem, I want to assign a specific time and date. But my problem is how can I implement it in my program? I've been following this post : A method to store date and time in Android using Firebase?
But the problem seems haven't yet solved.
Here's my program, this program function is when you borrow a certain item, you must assign what time and date you want to borrow that item. It is not yet fully developed because time assigning and date is my current problem.
public class Borrow extends AppCompatActivity implements DatePickerDialog.OnDateSetListener , TimePickerDialog.OnTimeSetListener{
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseBorrow;
private Button btnBorrow,btnPick;
private TextView tv_result;
private int day,month,year,hour,minute;
private int dayFinal,monthFinal,yearFinal,hourFinal,minuteFinal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_borrow);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() == null) {
finish();
startActivity(new Intent(this, Login.class));
}
Intent intent = getIntent();
btnPick = (Button) findViewById(R.id.btnPick);
tv_result = (TextView) findViewById(R.id.tvResult);
String id = intent.getStringExtra(SearchFragment.BOOK_ID);
btnBorrow=(Button)findViewById(R.id.borrowBtn);
databaseBorrow= FirebaseDatabase.getInstance().getReference("Borrow").child(id);
btnPick.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(Borrow.this , Borrow.this , year,month,day);
datePickerDialog.show();
}
});
btnBorrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button();
}
});
}
private void Button(){
String ids = databaseBorrow.push().getKey();
String buttonText = btnBorrow.getText().toString();
BorrowBook borrow = new BorrowBook(ids,buttonText);
databaseBorrow.child(ids).setValue(borrow);
Toast.makeText(this,"Borrow Successfully",Toast.LENGTH_SHORT).show();
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
yearFinal = i;
monthFinal = i1+1;
dayFinal = i2;
Calendar c= Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog =
new TimePickerDialog(Borrow.this, Borrow.this , hour , minute ,
DateFormat.is24HourFormat(this));
timePickerDialog.show();
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
hourFinal = i;
minuteFinal = i1;
//this portion of code is just a testing so that I can see what year and
date that i chose
tv_result.setText("year: "+ yearFinal + "\n" +
"month: "+ monthFinal + "\n" +
"day: "+ dayFinal + "\n" +
"hour: "+ hourFinal + "\n" +
"minute: "+ minuteFinal);
}
}
You need to create a Borrow Object (the borrow object can have check out and due date timestamps, item id, borrower, etc) and then assign the values that you get from the pickers to the fields. You then save the object to firebase in the way the firebase documents describe saving objects to firebase.