提问人:Zach T 提问时间:10/22/2023 最后编辑:Zach T 更新时间:10/23/2023 访问量:39
如何获取当前时间,当我按下 clockInOut 按钮时,要填充 RecyclerView 中的当前时间?
How do I get the current time to populate in the RecyclerView when I press the clockInOut button?
问:
在过去的 2 个月里,我一直在自学如何编码,并决定尝试制作一个计时应用程序。我检查了这段代码中的任何地方,我运行了日志,我什至问了 chatGPT,对于我来说,我无法弄清楚为什么这些时间不会填充 RecyclerViews。
我没有足够的空间来粘贴 .xml 文件,但如有必要,可以将其放在其他地方,或者我可以看看我是否可以只使用相关部分。
以下是相关文件:
TimeClockActivity.java
public class TimeClockActivity extends AppCompatActivity {
//declare Home Page Activity member variables (7txt_views, 3btn, 2edit_txt)
private Button toTimeEntryItemButton;
private Button backHomeButton;
private TextView timeClockTitleTextView;
private TextView hoursWorkedTodayTextView;
private TextView totalHoursWorkedTodayTextView;
private TextView hoursWorkedThisWeekTextView;
private TextView totalHoursWorkedWeekTextView;
private TextView projectedPayTextView;
private TextView totalProjectedPayTextView;
private TextView dateTimeDisplay;
private Calendar calendar;
private SimpleDateFormat dateFormat;
private SimpleDateFormat timeFormat;
private String date;
private Button clockInButton;
private Button clockInAtButton;
private TextView[] clockedInTextViews;
private TextView[] clockedOutTextViews;
private Switch[] lunchSwitches;
private boolean isClockIn = false;
private Map<String, TimeEntry> dayToTimeEntry = new HashMap<>();
private RecyclerView[] recyclerViews;
// private TimeEntryAdapter[] adapters;
private TimeEntryAdapter clockedInAdapter;
private TimeEntryAdapter clockedOutAdapter;
List<TimeEntry> mondayTimeEntries = new ArrayList<>();
List<TimeEntry> tuesdayTimeEntries = new ArrayList<>();
List<TimeEntry> wednesdayTimeEntries = new ArrayList<>();
List<TimeEntry> thursdayTimeEntries = new ArrayList<>();
List<TimeEntry> fridayTimeEntries = new ArrayList<>();
List<TimeEntry> saturdayTimeEntries = new ArrayList<>();
List<TimeEntry> sundayTimeEntries = new ArrayList<>();
// @SuppressLint("MissingInflatedId")
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_clock);
initializeViews();
initializeDateAndTime();
initializeRecyclerViews();
setClickListeners();
// Initialize adapters for each day of the week
TimeEntryAdapter mondayAdapter = new TimeEntryAdapter(mondayTimeEntries, "Monday");
TimeEntryAdapter tuesdayAdapter = new TimeEntryAdapter(tuesdayTimeEntries, "Tuesday");
TimeEntryAdapter wednesdayAdapter = new TimeEntryAdapter(wednesdayTimeEntries, "Wednesday");
TimeEntryAdapter thursdayAdapter = new TimeEntryAdapter(thursdayTimeEntries, "Thursday");
TimeEntryAdapter fridayAdapter = new TimeEntryAdapter(fridayTimeEntries, "Friday");
TimeEntryAdapter saturdayAdapter = new TimeEntryAdapter(saturdayTimeEntries, "Saturday");
TimeEntryAdapter sundayAdapter = new TimeEntryAdapter(sundayTimeEntries, "Sunday");
// Set the adapters on their respective RecyclerViews
RecyclerView recyclerMondayClockedIn = findViewById(R.id.recycler_monday_clocked_in);
RecyclerView recyclerTuesdayClockedIn = findViewById(R.id.recycler_tuesday_clocked_in);
RecyclerView recyclerWednesdayClockedIn = findViewById(R.id.recycler_wednesday_clocked_in);
RecyclerView recyclerThursdayClockedIn = findViewById(R.id.recycler_thursday_clocked_in);
RecyclerView recyclerFridayClockedIn = findViewById(R.id.recycler_friday_clocked_in);
RecyclerView recyclerSaturdayClockedIn = findViewById(R.id.recycler_saturday_clocked_in);
RecyclerView recyclerSundayClockedIn = findViewById(R.id.recycler_sunday_clocked_in);
recyclerMondayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerTuesdayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerWednesdayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerThursdayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerFridayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerSaturdayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerSundayClockedIn.setLayoutManager(new LinearLayoutManager(this));
recyclerMondayClockedIn.setAdapter(mondayAdapter);
recyclerTuesdayClockedIn.setAdapter(tuesdayAdapter);
recyclerWednesdayClockedIn.setAdapter(wednesdayAdapter);
recyclerThursdayClockedIn.setAdapter(thursdayAdapter);
recyclerFridayClockedIn.setAdapter(fridayAdapter);
recyclerSaturdayClockedIn.setAdapter(saturdayAdapter);
recyclerSundayClockedIn.setAdapter(sundayAdapter);
Log.d("TimeClockActivity", "Sunday Time Entries count: " + sundayTimeEntries.size());
}
// apply basic page instances
private void initializeViews() {
toTimeEntryItemButton = findViewById(R.id.btn_to_time_entry_item);
backHomeButton = findViewById(R.id.btn_to_home);
hoursWorkedTodayTextView = findViewById(R.id.tv_hours_worked_today_text);
totalHoursWorkedTodayTextView = findViewById(R.id.tv_hours_worked_today);
hoursWorkedThisWeekTextView = findViewById(R.id.tv_hours_worked_this_week_text);
totalHoursWorkedWeekTextView = findViewById(R.id.tv_hours_worked_this_week);
projectedPayTextView = findViewById(R.id.tv_projected_paycheck_text);
totalProjectedPayTextView = findViewById(R.id.tv_projected_paycheck);
dateTimeDisplay = findViewById(R.id.tv_date_and_time);
clockInButton = findViewById(R.id.btn_clock_in);
clockInAtButton = findViewById(R.id.btn_clock_in_at);
timeClockTitleTextView = findViewById(R.id.tv_time_clock_title);
lunchSwitches = new Switch[]{
findViewById(R.id.switch_monday_lunch),
findViewById(R.id.switch_tuesday_lunch),
findViewById(R.id.switch_wednesday_lunch),
findViewById(R.id.switch_thursday_lunch),
findViewById(R.id.switch_friday_lunch),
findViewById(R.id.switch_saturday_lunch),
findViewById(R.id.switch_sunday_lunch)
};
}
private void initializeDateAndTime() {
//apply the instances created for calendar and time
calendar = Calendar.getInstance();
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm aa");
timeFormat = new SimpleDateFormat("HH:mm - E");
updateDateTimeDisplay();
}
private void updateDateTimeDisplay() {
// date = dateFormat.format(calendar.getTime());
String date = timeFormat.format(new Date());
dateTimeDisplay.setText(date);
}
private void initializeRecyclerViews() {
RecyclerView[] clockedInRecyclerViews = new RecyclerView[]{
findViewById(R.id.recycler_monday_clocked_in),
findViewById(R.id.recycler_tuesday_clocked_in),
findViewById(R.id.recycler_wednesday_clocked_in),
findViewById(R.id.recycler_thursday_clocked_in),
findViewById(R.id.recycler_friday_clocked_in),
findViewById(R.id.recycler_saturday_clocked_in),
findViewById(R.id.recycler_sunday_clocked_in)
};
LinearLayoutManager[] layoutManagerArrayClockedIn = new LinearLayoutManager[]{
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this)
};
for (int i = 0; i < clockedInRecyclerViews.length; i++) {
clockedInRecyclerViews[i].setLayoutManager(layoutManagerArrayClockedIn[i]);
}
RecyclerView[] clockedOutRecyclerViews = new RecyclerView[]{
findViewById(R.id.recycler_monday_clocked_out),
findViewById(R.id.recycler_tuesday_clocked_out),
findViewById(R.id.recycler_wednesday_clocked_out),
findViewById(R.id.recycler_thursday_clocked_out),
findViewById(R.id.recycler_friday_clocked_out),
findViewById(R.id.recycler_saturday_clocked_out),
findViewById(R.id.recycler_sunday_clocked_out)
};
LinearLayoutManager[] layoutManagerArrayClockedOut = new LinearLayoutManager[]{
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this),
new LinearLayoutManager(this)
};
for (int i = 0; i < clockedOutRecyclerViews.length; i++) {
clockedOutRecyclerViews[i].setLayoutManager(layoutManagerArrayClockedOut[i]);
}
clockedInAdapter = new TimeEntryAdapter(new ArrayList<>(), "clockedIn");
clockedOutAdapter = new TimeEntryAdapter(new ArrayList<>(), "clockedOut");
for (int i = 0; i < clockedInRecyclerViews.length; i++) {
clockedInRecyclerViews[i].setLayoutManager(new LinearLayoutManager(this));
clockedInRecyclerViews[i].setAdapter(clockedInAdapter);
}
for (int i = 0; i < clockedOutRecyclerViews.length; i++) {
clockedOutRecyclerViews[i].setLayoutManager(new LinearLayoutManager(this));
clockedOutRecyclerViews[i].setAdapter(clockedOutAdapter);
}
}
private void setClickListeners() {
// set button click ability for the 'back home' button
backHomeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackHomeClick(v);
}
});
// set button click ability for the 'clock in' button
clockInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClockInButtonClick(v);
}
});
// set button click ability for the 'to time entry item' button
toTimeEntryItemButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onToTimeEntryItemButtonClick(v);
}
});
}
private void onToTimeEntryItemButtonClick(View view) {
Intent intent = new Intent(this, TimeEntry.class);
startActivity(intent);
}
// give the 'back home' button some functionality
private void onBackHomeClick(View v) {
Intent intent = new Intent(this, HomePageActivity.class);
startActivity(intent);
}
// give the clock in button some functionality
public void onClockInButtonClick(View view) {
// Determine the current day
int dayOfTheWeek = calendar.get(Calendar.DAY_OF_WEEK);
String day = getDayName(dayOfTheWeek);
Log.d("Time ClockActivity", "Clicked on day: " + day);
// Get or create a TimeEntry for the current day
TimeEntry timeEntry = dayToTimeEntry.get(day);
if (timeEntry == null) {
timeEntry = new TimeEntry(day);
dayToTimeEntry.put(day, timeEntry);
Log.d("TimeClockActivity", "Created new TimeEntry for day: " + day);
}
// Add clock-in or clock-out time
String currentTime = timeFormat.format(new Date());
Log.d("TimeClockActivity", "Current time: " + currentTime);
if (isClockIn) {
timeEntry.addClockInTime(currentTime);
} else {
timeEntry.addClockOutTime(currentTime);
}
clockedInAdapter.setTimeEntries(getClockedInTimeEntries());
clockedOutAdapter.setTimeEntries(getClockedOutTimeEntries());
clockedInAdapter.notifyDataSetChanged();
clockedOutAdapter.notifyDataSetChanged();
// After updating the data
Log.d("TimeClockActivity", "Clocked in time entries: " + getClockedInTimeEntries().size());
Log.d("TimeClockActivity", "Clocked out time entries: " + getClockedOutTimeEntries().size());
toggleClockInOutButtonText();
isClockIn = !isClockIn;
}
private List<TimeEntry> getClockedInTimeEntries() {
List<TimeEntry> clockedInTimeEntries = new ArrayList<>();
for (TimeEntry timeEntry : dayToTimeEntry.values()) {
if (timeEntry.hasClockInTime() && !timeEntry.hasClockOutTime()) {
clockedInTimeEntries.add(timeEntry);
}
}
return clockedInTimeEntries;
}
private List<TimeEntry> getClockedOutTimeEntries() {
List<TimeEntry> clockedOutTimeEntries = new ArrayList<>();
for (TimeEntry timeEntry : dayToTimeEntry.values()) {
if (timeEntry.hasClockOutTime()) {
clockedOutTimeEntries.add(timeEntry);
}
}
return clockedOutTimeEntries;
}
private void toggleClockInOutButtonText() {
if (isClockIn) {
clockInButton.setText("Clock In");
} else {
clockInButton.setText("Clock Out");
}
}
private String getDayName(int dayOfTheWeek) {
String day;
switch (dayOfTheWeek) {
case Calendar.SUNDAY:
day = "Sunday";
break;
case Calendar.MONDAY:
day = "Monday";
break;
case Calendar.TUESDAY:
day = "Tuesday";
break;
case Calendar.WEDNESDAY:
day = "Wednesday";
break;
case Calendar.THURSDAY:
day = "Thursday";
break;
case Calendar.FRIDAY:
day = "Friday";
break;
case Calendar.SATURDAY:
day = "Saturday";
break;
default:
day = "Unknown";
break;
}
return day;
}
时间条目.java
public TimeEntry() {
}
public TimeEntry(String day) {
this.day = day;
this.clockInTime = null;
this.clockOutTime = null;
this.clockInTimes = new ArrayList<>();
this.clockOutTimes = new ArrayList<>();
setDayOfWeek();
}
public void setDayOfWeek() {
SimpleDateFormat inputFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
SimpleDateFormat outputFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
try {
Date date = inputFormat.parse(day);
dayOfWeek = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
public String getClockInTime() {
return clockInTime;
}
public void setClockInTime(String clockInTime) {
this.clockInTime = clockInTime;
}
public String getClockOutTime() {
return clockOutTime;
}
public void setClockOutTime(String clockOutTime) {
this.clockOutTime = clockOutTime;
}
public boolean hasClockInTime() {
return clockInTime != null;
}
public boolean hasClockOutTime() {
return clockOutTime != null;
}
public List<String> getClockInTimes() {
return clockInTimes;
}
public List<String> getClockOutTimes() {
return clockOutTimes;
}
public String getDayOfWeek() {
return dayOfWeek;
}
public void addClockInTime(String clockInTime) {
Log.d("TimeEntry", "Added clock-in time: " + clockInTime);
clockInTimes.add(clockInTime);
}
public void addClockOutTime(String clockOutTime) {
Log.d("TimeEntry", "Added clock-out time: " + clockOutTime);
clockOutTimes.add(clockOutTime);
}
public void setDayOfWeek(String dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
}
时间条目适配器:.java
public class TimeEntryAdapter extends RecyclerView.Adapter<TimeEntryAdapter.TimeEntryViewHolder> {
private List<TimeEntry> timeEntries;
private String entryType;
// Constructor for the adapter
public TimeEntryAdapter(List<TimeEntry> timeEntries, String entryType) {
this.timeEntries = timeEntries;
this.entryType = entryType;
}
@NonNull
@Override
public TimeEntryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_entry_item, parent, false);
return new TimeEntryViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TimeEntryViewHolder holder, int position) {
TimeEntry timeEntry = timeEntries.get(position);
// Get references to the views within the item layout
TextView dayOfWeekTextView = holder.dayOfWeekTextView;
TextView clockedInTimeTextView = holder.clockedInTimeTextView;
TextView clockedOutTimeTextView = holder.clockedOutTimeTextView;
// Set data for the views based on the TimeEntry
dayOfWeekTextView.setText(timeEntry.getDayOfWeek()); // Set the day dynamically
clockedInTimeTextView.setText("Clock In: " + timeEntry.getClockInTime()); // Set clock-in time dynamically
clockedOutTimeTextView.setText("Clock Out: " + timeEntry.getClockOutTime()); // Set clock-out time dynamically
}
@Override
public int getItemCount() {
return timeEntries.size();
}
public static class TimeEntryViewHolder extends RecyclerView.ViewHolder {
TextView dayOfWeekTextView;
TextView clockedInTimeTextView;
TextView clockedOutTimeTextView;
public TimeEntryViewHolder(View itemView) {
super(itemView);
dayOfWeekTextView = itemView.findViewById(R.id.dayOfWeekTextView);
clockedInTimeTextView = itemView.findViewById(R.id.clockedInTimeTextView);
clockedOutTimeTextView = itemView.findViewById(R.id.clockedOutTimeTextView);
}
}
public void setTimeEntries(List<TimeEntry> timeEntries) {
this.timeEntries = timeEntries;
notifyDataSetChanged();
}
public void addEntry(TimeEntry entry) {
timeEntries.add(entry);
notifyDataSetChanged();
}
我只想按下 Clock In / Clock Out 按钮,让它为正确的日期填充正确的 Recyler View,以及它是时钟时间还是时钟时间。我让它对 TextViews 运行良好,然后决定“好吧,如果有人在同一天打卡和下班两次怎么办”。现在我正在对着我的电脑说废话。提前致谢,如果需要更多信息,请告诉我。
答:
0赞
avalerio
10/23/2023
#1
您永远不会更新适配器中的内部阵列列表 ->private List<TimeEntry> timeEntries;
将这样的功能添加到适配器中。
public void addEntry(TimeEntry entry) {
timeEntries.add(entry);
//This is a bad solution, but for your code structure and experience it will do.
notifyDataSetChanged();
}
该数组列表是回收器显示的内容,如果您从不添加它,它永远不会更改。
这样一来,如果您按星期几分隔视图,您就会遇到另一个问题,您需要每个回收器都有一个适配器,您不能重复使用相同的适配器,因为它包含它所显示的内容列表。如果每个 recyclerView 需要显示的内容不同,则它们不能是相同的适配器。
即你不能这样做
for (int i = 0; i < clockedInRecyclerViews.length; i++) {
clockedInRecyclerViews[i].setLayoutManager(new LinearLayoutManager(this));
clockedInRecyclerViews[i].setAdapter(clockedInAdapter); //<-- Bad
}
评论
0赞
Zach T
10/23/2023
感谢您的回复,我已经开始处理您的建议。我相信我将尝试使用 TimeEntryAdapter 和 'day'TimeEntries 实现 clockIn 和 clockOut 时间的开关情况。再次感谢您为我指明正确的方向,我希望尽快解决这个问题。
评论