Home > Software engineering >  Fragment - EditText with DatePickerDialog Fragment
Fragment - EditText with DatePickerDialog Fragment

Time:10-26

I have a fragment (FragmentAggiungiPaziente) that contains an EditText with which to select a date. When I select the date, the edittext does not update with the selected date. Could you kindly help me to solve this problem? Thanks

FragmentAggiungiPaziente

EditText nomePaz, cognomePaz, dataPaz, mailPaz, telefonoPaz;
Button aggiungiPaz;
DBHelper dbPaz;
View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_aggiungi_paziente, container, false);

    nomePaz = view.findViewById(R.id.nomePazNew);
    cognomePaz = view.findViewById(R.id.cognomePazNew);

    final FragmentManager fm = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
    dataPaz = view.findViewById(R.id.dataPazNew);

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

            FragmentData newFragment = new FragmentData();
            newFragment.show(fm, "data");
         
            getChildFragmentManager().setFragmentResultListener("chiaveRichiesta", getViewLifecycleOwner(), new FragmentResultListener() {
                @Override
                public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
                    String risultato = result.getString("chiaveRisultato");
                    dataPaz.setText(risultato);
                }
            });


        }

    });

    mailPaz = view.findViewById(R.id.mailPazNew);
    telefonoPaz = view.findViewById(R.id.telefonoPazNew);
    dbPaz = new DBHelper(getActivity());
    //
    aggiungiPaz = view.findViewById(R.id.aggiungiPazBtn);
    aggiungiPaz.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String nome = nomePaz.getText().toString();
            String cognome = cognomePaz.getText().toString();
            String data = dataPaz.getText().toString();
            String mail = mailPaz.getText().toString();
            String telefono = telefonoPaz.getText().toString();
            Sessione sessione = new Sessione(getActivity());
            String mail_medico = sessione.getSessione();

            if (nome.equals("") || cognome.equals("") || data.equals("") || mail.equals("") || telefono.equals("")){
                Toast.makeText(getActivity(), "Inserisci tutti i campi", Toast.LENGTH_SHORT).show();
            } else {
                boolean controlloPaz = dbPaz.controlloPaziente(mail_medico,nome,cognome,data,mail,telefono);
                if (controlloPaz==true){
                    boolean inserisci = dbPaz.inserisciPaziente(mail_medico, nome,cognome, data,mail,telefono);
                    if(inserisci==true){
                        Toast.makeText(getActivity(), "Paziente inserito con successo", Toast.LENGTH_SHORT).show();
                        getParentFragmentManager().beginTransaction().replace(R.id.frameLayoutMain,new FragmentListaPazienti()).addToBackStack(null).commit();
                    } else {
                        Toast.makeText(getActivity(), "Inserimento fallito", Toast.LENGTH_SHORT).show();
                    }
                } else {
                        Toast.makeText(getActivity(), "Paziente già inserito", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
    return view;
}

This one below is instead the fragment containing the date picker dialog. I used the setFragmentResultListener listener

FragmentData

public class FragmentData extends DialogFragment implements 
DatePickerDialog.OnDateSetListener {

final Calendar c = Calendar.getInstance();
@Override
 public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
    // i:anno, i1:mese, i2:giorno
    c.set(Calendar.YEAR, i);
    c.set(Calendar.MONTH, i1);
    c.set(Calendar.DAY_OF_MONTH, i2);
    String selectedDate = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALIAN).format(c.getTime());

   
    Bundle risultato = new Bundle();
    risultato.putString("chiaveRisultato",selectedDate);
    getParentFragmentManager().setFragmentResult("chiaveRichiesta", risultato);



}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    int anno = c.get(Calendar.YEAR);
    int mese = c.get(Calendar.MONTH);
    int giorno = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), FragmentData.this, anno, mese, giorno);
}

}

CodePudding user response:

Use FragmentManager instead of your getChildFragmentManager() Check below code:

     fm.setFragmentResultListener("chiaveRichiesta", getViewLifecycleOwner(), new FragmentResultListener() {
            @Override
            public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
                String risultato = result.getString("chiaveRisultato");
                dataPaz.setText(risultato);
            }
        });
  • Related