android-studioandroid-fragmentsandroid-spinnerandroid-datepickerandroid-imagebutton

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() {
        @Override
        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() {
                @Override
                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", 
                    Locale.JAPAN);
                    franchiseDate = formatter.format(chosendate);
                }
            }, year, month, dayOfMonth);
            datePickerDialog.show();
            datePickerDialog.getDatePicker().setMaxDate(cldr.getTimeInMillis());
        }

this is the spinner onItemSelected event:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

              
                @Override
                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);
                                spinner.setEnabled(false);
                               
                                Centre centre = new Centre();
                                centre.setArguments(bundle);
                                FragmentManager fragmentManager = getSupportFragmentManager();
                                FragmentTransaction fragmentTransaction = 
                                fragmentManager.beginTransaction();
                                fragmentTransaction.replace(R.id.multiFrame, Centre.class, 
                                bundle);
                                fragmentTransaction.commit();

                            }

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.


Solution

  • Resolved....call the fragment in onDateSet