get the time and minutes of a TimePickerDialog on frgaments android

0

Does anyone know how to get the Time and the minute of a TimePickerDialog? I have two buttons in a fragment, one that triggers a DataPickerDialog from which I get the date very well and a button that launches a TimePickerDialog but gives me an error when accepting once the selected time has been selected. How can I get the value and show it to me in a textview.

Here is my code:

public class AgendaFragment extends Fragment   {

public AgendaFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ((MainActivity) getActivity()).setActionbarTitle("Horario");
    View view = inflater.inflate(R.layout.fragment_agenda, container, false);



    Button btime = (Button) view.findViewById(R.id.btntime);
    Button bdate = (Button) view.findViewById(R.id.btndate);
    Button bsen = (Button) view.findViewById(R.id.btnsend);

    final TextView datvi = (TextView) view.findViewById(R.id.viewdate);
    final TextView tievi = (TextView) view.findViewById(R.id.viewti);
    final Calendar c = Calendar.getInstance();
    final int hora = c.get(Calendar.HOUR_OF_DAY);
    final int minuto = c.get(Calendar.MINUTE);


    bdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Calendar calendario = Calendar.getInstance();
            int yy = calendario.get(Calendar.YEAR);
            int mm = calendario.get(Calendar.MONTH);
            int dd = calendario.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    String fecha = String.valueOf(year) + "-" + String.valueOf(monthOfYear)
                            + "-" + String.valueOf(dayOfMonth);
                    datvi.setText(fecha);
                }
            }, yy, mm, dd);
            datePicker.show();
        }
    });


    btime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Calendar getDate = Calendar.getInstance();
            TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    getDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    getDate.set(Calendar.MINUTE, minute);
                    SimpleDateFormat timeformat=new SimpleDateFormat("HH:mm a");
                    timeformat.format(getDate.getTime());
                    tievi.setText((CharSequence) timeformat);
                }
            }, getDate.get(Calendar.HOUR_OF_DAY), getDate.get(Calendar.MINUTE), false);

            timePickerDialog.show();
        }
    });

    return view;

}
    
asked by Cesar 23.02.2018 в 05:34
source

1 answer

0

The error is in this part of here amigo.

btime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Calendar getDate = Calendar.getInstance();
            TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    getDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    getDate.set(Calendar.MINUTE, minute);
                    SimpleDateFormat timeformat=new SimpleDateFormat("HH:mm a");
                    timeformat.format(getDate.getTime());
                    tievi.setText((CharSequence) timeformat);
                }
            }, getDate.get(Calendar.HOUR_OF_DAY), getDate.get(Calendar.MINUTE), false);

            timePickerDialog.show();
        }
    });

Specifically in the code line

timeformat.format(getDate.getTime());

the method timeformat.format () returns a string with the date in the desired format, but here you do not assign that return to any variable. So when you want to convert this timeFormat to string, the application stops because that does not make sense. Change the code as follows

btime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Calendar getDate = Calendar.getInstance();
            TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    getDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    getDate.set(Calendar.MINUTE, minute);
                    SimpleDateFormat timeformat=new SimpleDateFormat("HH:mm a");
                    String formatedDate = timeformat.format(getDate.getTime());
                    tievi.setText(formatedDate);
                }
            }, getDate.get(Calendar.HOUR_OF_DAY), getDate.get(Calendar.MINUTE), false);

            timePickerDialog.show();
        }
    });

That should fix the problem, I hope it works for you,

    
answered by 23.02.2018 / 16:15
source