I have a Gridview with some times, which are pulled from a database. The Gridview is populated with Textviews (which show the times), which are tagged with the necessary information (ROW ID etc) to make changes to that time in the database. I have set an OnItemClickListener for the Gridview, which will load a TimePickerDialog via a DialogFragment.
// When items on the grid are pressed:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String type = (String) (v.getTag(R.id.VIEW_TAG_TIMEENTRY_TYPE));
String direction = (String) (v.getTag(R.id.VIEW_TAG_TIMEENTRY_DIRECTION));
long row_id = (long) v.getTag(R.id.VIEW_TAG_TIMEENTRY_ID);
// If entry doesn't exist
if (row_id < 0) {
DialogFragment dialogFragment = new TimePickerFragment();
// Tell the dialogFragment what to do when set......
dialogFragment.show(getSupportFragmentManager(), "timePicker");
}
}
});
The TimePickerFragment class is as follows:
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
In most of the examples I've seen, they just do something simple in the onTimeSet function, like update a specific textview. However I want the action taken to be a database add or update to the row corresponding to the textview that was clicked.
In C++ I would probably have passed a function that calls the update method in the ContentProvider as an argument to the constructor of this class, but I don't know the equivalent design pattern for Java.
One answer has mentioned an 'interface' might be the way to go, but I'm not sure how to implement this. (Results from a DialogFragment to an ActivityFragment)
Another answer mentioned using the setTargetFragment method, but the calling class is an activity in this case, so I can't do it. (Receive result from DialogFragment)
How should I best solve this design requirement? I've been wracking my android-app newbie brain quite a while.
Many thanks