diff --git a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java index 92d4908..ed88dab 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java @@ -42,7 +42,6 @@ private TodosViewModel todosViewModel; private BooksViewModel booksViewModel; - private Todo todo; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER @@ -63,6 +62,7 @@ //編集用 private boolean isEdit; private Todo editTodo; + private Book editBook; private int viwYear; private int viwMonth; private int viwDay; @@ -78,8 +78,9 @@ } //編集用コンストラクタ - public CreateTodoFragment(Todo editTodo) { + public CreateTodoFragment(Todo editTodo,Book editBook) { this.editTodo = editTodo; + this.editBook = editBook; this.isEdit = true; this.viwYear = editTodo.getYear(); this.viwMonth = editTodo.getMonth(); @@ -88,14 +89,6 @@ } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment CreateTodoFragment. - */ // TODO: Rename and change types and number of parameters public static CreateTodoFragment newInstance(String param1, String param2) { CreateTodoFragment fragment = new CreateTodoFragment(0,0,0); @@ -104,6 +97,7 @@ args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; + } @Override @@ -130,10 +124,10 @@ @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - selectBookNum = 0; EditText titleField = view.findViewById(R.id.todo_title_inputField); + titleField.setText(viwTitle); // 年の初期値を設定 NumberPicker numberPickerYear = view.findViewById(R.id.numberPickerYear); numberPickerYear.setMinValue(citrus.getCurYear()); @@ -167,14 +161,31 @@ Spinner spinner = view.findViewById(R.id.select_todo_spinner); bookTitles = new ArrayList<>(); bookTitles.add("本を選択"); + + // Adapterの初期化を先に行う + ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, bookTitles); + adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(HashMap integerBookHashMap) { if(integerBookHashMap != null){ for(int i : integerBookHashMap.keySet()){ bookTitles.add(integerBookHashMap.get(i).getTitle()); + // 編集の場合、BookIdから対応するタイトルを探す + if (isEdit && integerBookHashMap.get(i).getTitle().equals(editBook.getTitle())){ + selectBookNum = bookTitles.size() - 1; // 現在追加した位置を保持 + } } - }else{ + // ArrayAdapterの更新を通知 + adapter.notifyDataSetChanged(); + + // データがセットされた後にSpinnerに選択状態を設定 + if (isEdit) { + spinner.setSelection(selectBookNum); + } + } else { bookTitles = new ArrayList<>(); } } @@ -182,9 +193,11 @@ booksViewModel.loadBooks(accountId, token); - ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, bookTitles); - adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); - spinner.setAdapter(adapter); + // 編集モードの場合、事前に本が選択されるようにSpinnerに設定 + if (isEdit) { + spinner.setSelection(selectBookNum); + } + //キャンセルボタン処理 Button cancelButton = view.findViewById(R.id.todo_cancel_button); @@ -200,7 +213,9 @@ deleteButton.setVisibility(View.INVISIBLE); // 追加時ボタンを非表示 } deleteButton.setOnClickListener(v->{ - //ここにdelete処理か確認ダイアログ(いるかな) + todosViewModel.deleteTodo(accountId,editBook.getBookId(),editTodo.getYear(), + editTodo.getMonth(),editTodo.getDay(), editTodo.getTodoId(), token); + ((MainActivity)getActivity()).backFragment(); }); //追加ボタン処理