Difficulty in using DatePicker

4

I'm studying things for a project here and I'm trying to use DatePicker, I could implement it normally, but I want to use two in the same activity, putting a start date and an end date, I put two buttons that use the same DatePickerFragment.java, my xml looks like this:

<TextView
    android:id="@+id/textoData1ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="59dp"
    android:text="Data inicial"
    android:textSize="30dp" />

<Button
    android:id="@+id/botaoData1ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="118dp"
    android:text="Abrir texto inicial" />

<TextView
    android:id="@+id/textoData2ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="202dp"
    android:text="Data final"
    android:textSize="30dp" />

<Button
    android:id="@+id/botaoData2ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="268dp"
    android:text="Abrir data final" />

<TextView
    android:id="@+id/textoDataFinalID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="212dp"
    android:text="Dias entre elas"
    android:textSize="30dp" />

My idea is on each button to open a DatePicker and change the TextView to that date, and in the end make the count of days between the two dates and show in the third TextView. I tried to use a switch to change each one a TextView, and that's where I need help, what will the switch look at? How to differentiate the return of each button? And how do you count the amount of days between the two dates? Here is the code for the buttons:

  botaoData1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment datePicker1 = new DatePickerFragment();
            datePicker1.show(getSupportFragmentManager(), "date picker");
        }
    });

    botaoData2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment datePicker2 = new DatePickerFragment();
            datePicker2.show(getSupportFragmentManager(), "date picker");

        }

    });
}

DatePickerFragment code:

public class DatePickerFragment extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar c = Calendar.getInstance();
    int ano = c.get(Calendar.YEAR);
    int mes = c.get(Calendar.MONTH);
    int dia = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(),(DatePickerDialog.OnDateSetListener)getActivity(), ano,mes,dia);
}   }
    
asked by anonymous 01.06.2018 / 23:01

1 answer

0

I thought of the following proposal: Use DatePickerDialog in a custom class that will have an interface that will return a Celendar every time the user finalizes the date selection:

DatePickerCalendar.java

public class DatePickerCalendar {
    private OnDateSetListener onDateSetListener;
    private Calendar caledarioPadrao;
    private Context contexto;

    public DatePickerCalendar(Calendar calendarioPadrao, Context context) {
        this.caledarioPadrao = calendarioPadrao;
        this.contexto = context;
        show();
    }

    public interface OnDateSetListener {
        void onDateSet(Calendar calendario);
    }

    public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
        this.onDateSetListener = onDateSetListener;
    }

    private void show() {
        new DatePickerDialog(contexto, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Calendar calendarioSelecionado = Calendar.getInstance();
                calendarioSelecionado.set(year, month, dayOfMonth);
                onDateSetListener.onDateSet(calendarioSelecionado);
            }
        }, caledarioPadrao.get(Calendar.YEAR), caledarioPadrao.get(Calendar.MONTH), caledarioPadrao.get(Calendar.DAY_OF_MONTH));
    }
}

In your Activity that contains TextView

botaoData1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Calendar hoje = Calendar.getInstance();
        DatePickerCalendar pickerCalendar = new DatePickerCalendar(hoje, MainActivity.this);
        pickerCalendar.setOnDateSetListener(new DatePickerCalendar.OnDateSetListener() {
            @Override
            public void onDateSet(Calendar calendario) {
                TextView tv = findViewById(R.id.textView11);
                tv.setText(dateMillisToString(calendario.getTimeInMillis(), "dd/MM/yyyy"));
            }
        });            
    }
});

dateMillisToString

private String dateMillisToString(long millis, String pattern) {
    String s = "Data desconhecida";
    try  {
        DateFormat df = new SimpleDateFormat(pattern, Locale.getDefault());
        s = df.format(new Date(millis));
    } catch (Exception ignored) {}
    return s;
}
  

"And how do I count the number of days between the two dates?"

One day has 86400000 milliseconds, you can get calendario.getTimeInMillis() , subtract by calendario2.getTimeInMillis() and multiply back by 86400000 to get the difference in days

    
02.06.2018 / 17:25