Pass Date from Datepicker to Spinner selected item and load API Response in Fragment

I have a working app where user select an option from Spinner the datepicker auto open user select the date and view data. However, I am trying to include an Imagebutton(calendar) which opens a date picker and than on selection of date data should load. The process should be : select Option from spinner -> select date from datepicker by clicking Imagebutton -> load/display data.

this is the imagebutton onclick event:

imageButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            final Calendar cldr = Calendar.getInstance();
            int year = cldr.get(Calendar.YEAR);
            int month = cldr.get(Calendar.MONTH);
            int dayOfMonth = cldr.get(Calendar.DAY_OF_MONTH);

            // date picker dialog
            datePickerDialog = new DatePickerDialog(Dashboard.this, 
         AlertDialog.THEME_DEVICE_DEFAULT_DARK, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.set(year, month, dayOfMonth);
                    Date chosendate = calendar1.getTime();
                    calendar1.set(year, month, dayOfMonth);
                    chosendate = calendar1.getTime();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", 
                    franchiseDate = formatter.format(chosendate);
            }, year, month, dayOfMonth);

this is the spinner onItemSelected event:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> adapterView, View view, int 
                 position, long l) {

                    selectedItem = spinner.getSelectedItem().toString();
                    selectedPosition = spinner.getSelectedItemPosition();

                   if (selectedItem.equals("Revenue by Centre")) {

                         //   String centerDate = franchiseDate;

                                Bundle bundle = new Bundle();
                                bundle.putString("authToken", authToken);                                                                      
                                bundle.putString("centreDate", franchiseDate);
                                Centre centre = new Centre();
                                FragmentManager fragmentManager = getSupportFragmentManager();
                                FragmentTransaction fragmentTransaction = 
                                fragmentTransaction.replace(, Centre.class, 


Data is displayed in fragment, date is also a var that is passed to fragment class. If I am including spinner.setOnItemSelectedListener in imageButton.setOnClickListener event than data is loading after I select new date ie select a date no data again select a date data is loading for previously selected date.

1 answer

  • answered 2022-05-07 05:08 tintin the fragment in onDateSet

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum