Android ROOM- 我如何 运行 查询自定义日历中的每个单元格?
Android ROOM- How can I run a query on each cell inside my custom calendar?
我有一个customCalendarView.
为了创建这个,我使用了 ArrayAdapter class (MyGridAdapter)。
当用户点击日历中的一个方块时,他们会被带到一个 activity,他们可以在其中保存一个 logEntry,其中包含一些信息以及在日历上点击的日期。
此日志条目已保存到数据库中。
我想在特定日期出现日志条目时在日历中的每个方块上显示一个小圆圈。
我在我的 logEntriesDao 中创建了一个查询,其中 returns 所有日志的日期都与传递给它的日期相同。
@Query("SELECT * FROM log_entries_table WHERE log_entries_table.date = :date " )
LiveData<List<Log_Entries>> getAllFromWorkoutLogEntriesonDate(String date);
我如何 运行 在我的日历中的每个单元格上进行此查询,然后在每个存在日志的日历单元格上将我的圆形 imageView 可见性设置为 VISIBLE?
CustomCalendarView
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;
}
}
考虑下一个通用模式:
- 您的查询请求应该是您日历上所有可见日期的一次性拍摄。否则不是最优的。我认为您可以获得日期范围(例如 01.05.20..31.05.20)或日期列表(01.05,02.05,...,31.05)并将此范围(列表)设置为查询的参数(列表日期或 date1|date2 的范围)。
- 您应该更改查询以获取日期列表,其中存在日志。假设您有 6 个带有日志的日期,那么您的查询应该 return 列出 6 个日期。
- 如果您使用的是 MVVM 模式,您可以在 Activity 观察来自 ROOM 的 LiveData-result(观察它,您将获得 activity 创建的 6 个日期和 7 个日期的列表在你用你的第二个 activity).
添加一些日志之后
- 然后您可以将您的日期列表设置到您的适配器并在那里实施您的 UI-更改(圆圈,背景等等)。
我有一个customCalendarView.
为了创建这个,我使用了 ArrayAdapter class (MyGridAdapter)。
当用户点击日历中的一个方块时,他们会被带到一个 activity,他们可以在其中保存一个 logEntry,其中包含一些信息以及在日历上点击的日期。
此日志条目已保存到数据库中。
我想在特定日期出现日志条目时在日历中的每个方块上显示一个小圆圈。
我在我的 logEntriesDao 中创建了一个查询,其中 returns 所有日志的日期都与传递给它的日期相同。
@Query("SELECT * FROM log_entries_table WHERE log_entries_table.date = :date " )
LiveData<List<Log_Entries>> getAllFromWorkoutLogEntriesonDate(String date);
我如何 运行 在我的日历中的每个单元格上进行此查询,然后在每个存在日志的日历单元格上将我的圆形 imageView 可见性设置为 VISIBLE?
CustomCalendarView
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;
}
}
考虑下一个通用模式:
- 您的查询请求应该是您日历上所有可见日期的一次性拍摄。否则不是最优的。我认为您可以获得日期范围(例如 01.05.20..31.05.20)或日期列表(01.05,02.05,...,31.05)并将此范围(列表)设置为查询的参数(列表日期或 date1|date2 的范围)。
- 您应该更改查询以获取日期列表,其中存在日志。假设您有 6 个带有日志的日期,那么您的查询应该 return 列出 6 个日期。
- 如果您使用的是 MVVM 模式,您可以在 Activity 观察来自 ROOM 的 LiveData-result(观察它,您将获得 activity 创建的 6 个日期和 7 个日期的列表在你用你的第二个 activity). 添加一些日志之后
- 然后您可以将您的日期列表设置到您的适配器并在那里实施您的 UI-更改(圆圈,背景等等)。