Android日历:将点距设置为日历中来自MySQL数据库的日期

时间:2018-07-23 07:17:28

标签: android android-calendar

我创建了一个日历,如果单击日期,它将显示一项或多项活动。如果当天没有任何活动,则不会显示任何内容。

like this

但是当我尝试在当天下打点时,我遇到了问题,因为我的活动仅在单击日期后才显示。因此,如果我没有单击它,它将不加载任何内容。重点是如何使日历中的活动从数据库中读取,并在日期有活动时显示一个点距。

这是我的代码

 calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @SuppressLint("ResourceAsColor")
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            if ((month + 1) < 10 && dayOfMonth < 10) {
                String date = year + "-0" + (month + 1) + "-0" + dayOfMonth;
                tv_date.setText(date);
            } else if ((month + 1) > 9 && dayOfMonth < 10) {
                String date = year + "-" + (month + 1) + "-0" + dayOfMonth;
                tv_date.setText(date);
            } else if ((month + 1) < 10 && dayOfMonth > 9) {
                String date = year + "-0" + (month + 1) + "-" + dayOfMonth;
                tv_date.setText(date);
            } else {
                String date = year + "-" + (month + 1) + "-" + dayOfMonth;
                tv_date.setText(date);
            }

            LoginUser savedUser = new Gson()
                    .fromJson(getActivity().getSharedPreferences(MY_LOGIN_PREF, Context.MODE_PRIVATE)
                            .getString(MY_LOGIN_PREF_KEY, ""), LoginUser.class);

            String salesmanId = savedUser.getSalesmanId();

            String startDate = tv_date.getText().toString()+" 00:00:00";
            String endDate = tv_date.getText().toString()+" 23:59:00";

            ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Loading activity...");
            progressDialog.show();

            API.getCalendar(salesmanId,startDate, endDate).enqueue(new Callback<ArrayList<ActivitySalesmenAdd>>() {
                @Override
                public void onResponse(Call<ArrayList<ActivitySalesmenAdd>> call, Response<ArrayList<ActivitySalesmenAdd>> response) {
                    if (response.code() == 200) {
                        tempData = response.body();
                        if (tempData.isEmpty()){
                            emptyData.setVisibility(View.VISIBLE);
                            recyclerView.setVisibility(View.GONE);
                        }else {
                            emptyData.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.VISIBLE);
                        }
                        recyclerView.setHasFixedSize(true);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        recyclerView.setAdapter(new CalendarSalesmanAdapter(tempData));
                        progressDialog.dismiss();
                    } else {
                        progressDialog.dismiss();
                        Toast.makeText(getActivity(), "Mohon cek koneksi internet anda", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<ArrayList<ActivitySalesmenAdd>> call, Throwable t) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(), "Failed", Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

我还完成了创建API的工作,以获取带有参数year和month的数据: like this

0 个答案:

没有答案