I have onItemClickListener for the ListView that opens DatePicker dialog when an item is clicked:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showDatePickerDialog(view);
}
});
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
After date in DatePicker gets set, I have onDateSet method which I want to use to update database and set date for item that was clicked in ListView:
public void onDateSet(DatePicker view, int year, int month, int day) {
...
dataSource.updateDate(adapter.getItemId(position), month + "/" + day + "/" + year);
...
}
How can I pass ListView position of the item that was clicked to onDateSet so that my database helper knows which record to update?
EDIT: Here's the DatePickerFragment class:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
...
dataSource.updateDate(adapter.getItemId(position), month + "/" + day + "/" + year);
...
}
}
Thanks!