diff --git a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java index 73ddb7d..7ce0a8c 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -35,11 +35,17 @@ import java.security.PrivateKey; import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + /** * A simple {@link Fragment} subclass. * Use the {@link HomeFragment#newInstance} factory method to @@ -299,12 +305,21 @@ } public void setTodos(List todos ,HashMap idBookHashMap) { +// Collections.sort(books, new Comparator() { +// @Override +// public int compare(Book b1, Book b2) { +// return Integer.compare(b1.getBookId(), b2.getBookId()); +// } +// }); todoList = todos; if (idBookHashMap != null) { this.idBookHashMap = new HashMap<>(idBookHashMap); } else { this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 } + + sortTodosById(); + notifyDataSetChanged(); } @@ -352,10 +367,21 @@ return todoList.size(); } - public void sortTodosById(Todo todo) { - notifyDataSetChanged(); + private void sortTodosById() { + Collections.sort(todoList, new Comparator() { + @Override + public int compare(Todo todo1, Todo todo2) { + int bookComparison = Integer.compare(todo1.getBookId(), todo2.getBookId()); + if (bookComparison != 0) { + return bookComparison; // bookIdでの比較 + } + return Integer.compare(todo1.getTodoId(), todo2.getTodoId()); // 同じbookIdの場合、todoIdで比較 + } + }); + notifyDataSetChanged(); // データ変更をアダプタに通知 } + static class MyTodoViewHolder extends RecyclerView.ViewHolder { Button todoButton; CheckBox todoCheckBox; @@ -403,6 +429,8 @@ int blue = 169; Schedule scheduleData = this.scheduleList.get(position); holder.scheduleButton.setText(scheduleData.getTitle()); +// holder.scheduleButton.setText(scheduleData.getTitle() + extractionTime(scheduleData.getStartTime()) + " ~ " + extractionTime(scheduleData.getEndTime())); + System.out.println(scheduleData.getEndTime()); Book book = idBookHashMap.get(scheduleData.getBookId()); if(book != null) { red = Integer.parseInt(book.getColor().substring(1, 3), 16); @@ -414,6 +442,46 @@ } +// private void sortScheduleByTime() { +// Collections.sort(scheduleList, new Comparator() { +// @Override +// public int compare(Schedule schedule1, Schedule schedule2) { +// return Integer.compare(schedule1.getStartTime(), schedule2.getStartTime()); +// } +// }); +// notifyDataSetChanged(); // データ変更をアダプタに通知 +// } + + private static int convertTimeInteger(String scheduleTime) { + try { + // フォーマッタを定義 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"); + + // StringをLocalDateTimeに変換 + LocalDateTime dateTime = LocalDateTime.parse(scheduleTime, formatter); + + // 年、月、日、時、分を抽出 + int year = dateTime.getYear(); + int month = dateTime.getMonthValue(); + int day = dateTime.getDayOfMonth(); + int hour = dateTime.getHour(); + int minute = dateTime.getMinute(); + + // 必要に応じて整数を組み合わせる(例: YYYYMMDDHHMM) + int result = year * 1000000 + month * 10000 + day * 100 + hour * 10 + minute; + + return result; + } catch (DateTimeParseException e) { + e.printStackTrace(); + return -1; // エラーが発生した場合は-1を返す + } + } + + private static int extractionTime(String scheduleTime) { + int time = convertTimeInteger(scheduleTime); + int result = time % 10000; + return result; + } @Override public int getItemCount() {