0

I want to pass the data from the API (called using Retrofit) to calendar date onClick() event (onSelectedDayChange).

public class MainActivity extends AppCompatActivity {
List<APIData> dataList;
private CalendarView mCalendarView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mCalendarView=  findViewById(R.id.calendarView);
    Calendar rightNow = Calendar.getInstance();
    mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            month = 1+month;
            Toast.makeText(getApplicationContext(), ""+year+"-"+month+"-"+dayOfMonth,Toast.LENGTH_SHORT).show();

        }
    });


    APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
    Call<APIData> call = apiInterface.gethistorydetails("s10-1", "2018-08-01");
    call.enqueue(new Callback<APIData>() {
        @Override
        public void onResponse(Call<APIData> call, Response<APIData> response) {
            Log.d("TAG",response.code()+"");
            APIData apiData = response.body();
        }
        @Override
        public void onFailure(Call<APIData> call, Throwable t) {

        }
    });

}

}

As you can see in this pic, there are 4 different fields where I need to show data called from API. So whenever I click any dates on the calendar, the respective data from API should fill up the fields.

Dipesh
  • 1
  • 4

2 Answers2

0

Assuming you are having all the 4 dates from APIData object. Convert the date(2018-09-09) into timeInMillis and set the date into calendarView on click of respective date fields:-

bus_arrival.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCalendarView.setDate(timeInMillis);
        }
    });
check_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendarView.setDate(timeInMillis);
            }
        });
Nainal
  • 1,728
  • 14
  • 27
  • { "id": 9, "day": "2018-08-15", "type": "R", "check_in": "03:08:32", "check_out": null, "reach_home": 0, "bus_coming": 0, "reach_school": 0, "hist_station": [] }, I only have one date.And from that date , i have to call the other 4 data.. – Dipesh Sep 19 '18 at 04:11
  • Do you want to set this **"day": "2018-08-15"** date in calendar? – Nainal Sep 19 '18 at 07:07
  • Nope..I need select this date in calendar and the respective data should fill up the other 4 fields.. – Dipesh Sep 19 '18 at 14:30
0

In this use case you have to call the api every time whenever you change the date in calendar, This api will give you data in its response, simply set this data in your fields.

There are few steps to follow.

1- In datePicker dailog's onSelectedDayChange method you are getting "int year, int month, int dayOfMonth". Convert these fields into that date format that your API can understand. To know how to convert these fields into date format follow this link Creating java date object from year,month,day

2- Now call this APi , this api will return you result of those 4 fields in its response.

3- Simply display this data on those fields.

You code will be liked that

    @Override
    public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
         Calendar cal= Calendar.getInstance();
         cal.set(year, month - 1, dayOfMonth, 0, 0);  
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         String date=format.formate(cal.getTime());


         APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
         Call<APIData> call = apiInterface.gethistorydetails("s10-1", date);
         call.enqueue(new Callback<APIData>() {
            @Override
            public void onResponse(Call<APIData> call, Response<APIData> response) {
                Log.d("TAG",response.code()+"");
                APIData apiData = response.body();

                // Here you will get data from this api and display it on your required fields
               }
           @Override
            public void onFailure(Call<APIData> call, Throwable t) {

           }
});
yasir shehzad
  • 49
  • 1
  • 5