Search code examples
androidandroid-arrayadapterandroid-timepicker

get the time picked from TimePicker called from Custom ArrayAdapter


I have an activity which implements OnTimeSetListener and in which I declared a TimePickerFragment :

public static 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(),
                (MainActivity) getActivity(), hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub

    }

}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // TODO Auto-generated method stub
    pickerHour = hourOfDay;
    pickerMin = minute;
    ((TextView) findViewById(R.id.textView_hour)).setText(this.pickerHour
            + ":" + this.pickerMin);
}

And I call this TimePicker from a custom ArrayAdapter :

TextView hour = (TextView) includeBottom.findViewById(R.id.textView_hour);
        hour.setText(Content.FORMAT_HEURES_RAPPEL.format(rappel.getHeure()));
        hour.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RappelAdapter.this.runAnimation2(v);
                DialogFragment newFragment = new TimePickerFragment();
                newFragment.show(((MainActivity)contexte).getSupportFragmentManager(), "timePicker");

            }
        });

How can I need to save the hour in database, so I need to know the time picked from the user from the adapter. How can I do this?


Solution

  • You can use SharedPreferences to save hour and min and get it any time in int or string save hour and min from String or int into SharedPreferences