【发布时间】:2020-05-11 13:56:00
【问题描述】:
我有一个customCalendarView。
为了创建它,我使用了一个 ArrayAdapter 类 (MyGridAdapter)。
当用户点击日历中的一个方块时,他们会被带到一个活动中,他们可以在其中保存一个 logEntry,其中包含一些信息以及在日历上点击的日期。
此日志条目被保存到数据库中。
当特定日期存在日志条目时,我想在日历的每个方块上显示一个小圆圈。
我在 logEntriesDao 中创建了一个查询,它返回与传递给它的日期相同日期的所有日志。
@Query("SELECT * FROM log_entries_table WHERE log_entries_table.date = :date " )
LiveData<List<Log_Entries>> getAllFromWorkoutLogEntriesonDate(String date);
如何在日历中的每个单元格上运行此查询,然后在存在日志的每个日历单元格上将我的圆形 imageView 可见性设置为 VISIBLE?
自定义日历视图
public class CustomCalendarView extends LinearLayout {
ImageButton NextButton, PreviousButton;
TextView CurrentDate;
GridView gridView;
public static final int MAX_CALENDAR_DAYS = 42;
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
Context context;
MyGridAdapter myGridAdapter;
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", Locale.ENGLISH);
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.ENGLISH);
// SimpleDateFormat eventDateFormat = new SimpleDateFormat(("yyyy-MM-dd"),Locale.ENGLISH);
SimpleDateFormat eventDateFormat = new SimpleDateFormat(("dd-MM-yyyy"),Locale.ENGLISH);
public static final String MY_PREFS_NAME = "MyPrefsFile";
List<Date> dates = new ArrayList<>();
List<Log_Entries> eventsList = new ArrayList<>();
public CustomCalendarView(Context context) {
super(context);
}
public CustomCalendarView(final Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
InitializeLayout();
SetUpCalendar();
PreviousButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH, -1);
SetUpCalendar();
}
});
NextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH, 1);
SetUpCalendar();
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
final String date = eventDateFormat.format(dates.get(position));
Intent i = new Intent(getContext(), WorkoutButtonsActivity.class);
i.putExtra(WorkoutButtonsActivity.EXTRA_DATE, date);
getContext().startActivity(i);
}
});
}
public CustomCalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr ) {
super(context, attrs, defStyleAttr);
}
private void InitializeLayout(){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.calendar_layout, this);
NextButton = view.findViewById(R.id.nextBtn);
PreviousButton = view.findViewById(R.id.previousBtn);
CurrentDate = view.findViewById(R.id.current_Date);
gridView = view.findViewById(R.id.gridview);
}
private void SetUpCalendar(){
String currentDate = dateFormat.format(calendar.getTime());
CurrentDate.setText(currentDate);
dates.clear();
Calendar monthCalendar= (Calendar) calendar.clone();
monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK) -1;
monthCalendar.add(Calendar.DAY_OF_MONTH, - FirstDayofMonth);
while (dates.size() < MAX_CALENDAR_DAYS){
dates.add(monthCalendar.getTime());
monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
myGridAdapter = new MyGridAdapter(context,dates,calendar,eventsList);
gridView.setAdapter(myGridAdapter);
}
}
MyGridAdapter
public class MyGridAdapter extends ArrayAdapter {
List<Date> dates;
Calendar currentDate;
List<Log_Entries> logs;
LayoutInflater inflater;
public MyGridAdapter(@NonNull Context context, List<Date> dates, Calendar currentDate, List<Log_Entries> logs){
super(context, R.layout.single_cell_layout);
this.dates = dates;
this.currentDate = currentDate;
this.logs = logs;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Date monthDate = dates.get(position);
Calendar dateCalendar = Calendar.getInstance();
dateCalendar.setTime(monthDate);
int DayNo = dateCalendar.get(Calendar.DAY_OF_MONTH);
int displayMonth = dateCalendar.get(Calendar.MONTH) +1;
int displayYear = dateCalendar.get(Calendar.YEAR);
int currentMonth = currentDate.get(Calendar.MONTH) + 1;
int currentYear = currentDate.get(Calendar.YEAR);
int currentDay = currentDate.get(Calendar.DAY_OF_MONTH);
View view = convertView;
if (view == null){
view = inflater.inflate(R.layout.single_cell_layout, parent,false);
}
if (displayMonth == currentMonth && displayYear == currentYear){
view.setBackgroundColor(getContext().getResources().getColor(R.color.green));
}
else{
view.setBackgroundColor(Color.parseColor("#cccccc"));
}
TextView Day_Number = view.findViewById(R.id.calendar_day);
Day_Number.setText(String.valueOf(DayNo));
Calendar eventCalendar = Calendar.getInstance();
if(DayNo == currentDay && displayMonth == eventCalendar.get(Calendar.MONTH) + 1 && displayYear == eventCalendar.get(Calendar.YEAR)){
Day_Number.setTextColor(Color.parseColor("#FFFF33"));
}
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < logs.size(); i++){
}
return view;
}
@Override
public int getCount() {
return dates.size();
}
@Override
public int getPosition(@Nullable Object item) {
return dates.indexOf(item);
}
@Nullable
@Override
public Object getItem(int position) {
return dates.get(position);
}
}
LogEntries 实体
@Entity(tableName = "log_entries_table")
public class Log_Entries {
@PrimaryKey(autoGenerate = true)
int log_id;
@ForeignKey(entity = Junction.class, parentColumns = "exercise_workout_id", childColumns = "junction_id")
private int junction_id;
@ForeignKey(entity = Junction.class, parentColumns = "workout_id", childColumns = "workout_id")
private int workout_id;
double total_weight_lifted;
int set_number;
int reps;
public String date;
public Log_Entries(int junction_id, int workout_id, double total_weight_lifted, int set_number, int reps, String date) {
this.junction_id = junction_id;
this.workout_id = workout_id;
this.total_weight_lifted = total_weight_lifted;
this.set_number = set_number;
this.reps = reps;
this.date = date;
}
public void setLog_id(int log_id) {
this.log_id = log_id;
}
public int getLog_id() {
return log_id;
}
public int getJunction_id() {
return junction_id;
}
public double getTotal_weight_lifted() {
return total_weight_lifted;
}
public void setTotal_weight_lifted(double total_weight_lifted) {
this.total_weight_lifted = total_weight_lifted;
}
public int getSet_number() {
return set_number;
}
public int getReps() {
return reps;
}
public int getWorkout_id() {
return workout_id;
}
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return date;
}
}
【问题讨论】:
标签: java android android-sqlite android-room android-calendar