我有一个onItemClickListener用于ListView,当项目被点击时会打开DatePicker对话框:
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");
}
在DatePicker中设置日期后,我有一个onDateSet方法,我想使用它来更新数据库并设置ListView中所点击项目的日期:
public void onDateSet(DatePicker view, int year, int month, int day) {
...
dataSource.updateDate(adapter.getItemId(position), month + "/" + day + "/" + year);
...
}
如何将被点击的项目的 ListView 位置 传递给 onDateSet ,以便我的数据库帮助程序知道要更新哪条记录?
编辑: 这是 DatePickerFragment 类:
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);
...
}
}
谢谢!
onDateSet(DatePicker, int, int, int)
属于您定义的接口吗?还是 Android 定义的? - gunarDatePickerDialog.OnDateSetListener
中,它是由Android定义的。 - AlexLocalDatePickerListener
并使用我描述的相同方法。不同之处在于DatePickerListener
由片段实现,而LocalDatePickerListener
将由宿主活动实现。 - gunar