Em tạo DatePickerDialog trong Fragment để dùng lại nhiều lần, nhưng lại chưa biết cách lấy Date mới sau khi set để cập nhập lại TextView trong Activity. Mọi người chỉ giúp em cách làm với ạ, em cảm ơn nhiều!
Lấy dữ liệu trong DatePickerDialog
set onDateSetListener cho nó thôi
What the hell reusing code! 
Như thế này ạ:
Fragment:
public class DatePickerFragment extends android.support.v4.app.DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), null, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
}
Activity:
DialogFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "datePicker");
Giờ em muốn lấy cái giá trị Date sau khi set thì thì phải làm sao ạ?
- phải gắn cái listener đó vào trong DatePickerDialog thì mới có tác dụng.
- minor kết quả ra activity bằng callback
- xem xét xem có cần học thêm về javacore trước khi quay lại làm Android không, mình thấy bạn đang bị hổng kiến thức quá. Làm ngay Android thì cực lắm
1 Like
Cảm ơn bạn, mình cũng mới học 1 chút javacore, đang tập làm mấy app đơn giản để hiểu thêm ^^
Bạn cho mình hỏi DatePickerDialog nếu mình muốn tạo 1 lần và dùng lại cho nhiều activity thì làm cách nào nhỉ?
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?