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 e97be04..a980aa1 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import android.view.LayoutInflater; @@ -18,11 +19,15 @@ import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; import com.example.citrusclient.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.example.citrusclient.viewmodels.TodosViewModel; +import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; +import java.util.List; /** * A simple {@link Fragment} subclass. @@ -43,11 +48,11 @@ private String mParam1; private String mParam2; + List bookTitles; + private Citrus citrus; - private int year; - private int month; - private int day; - private String title; + private String accountId; + private String token; private int selectBookNum; public CreateTodoFragment() { @@ -81,6 +86,8 @@ } citrus = (Citrus)(getActivity().getApplication()); + accountId = citrus.getAccountId(); + token = citrus.getToken(); todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); } @@ -98,7 +105,6 @@ selectBookNum = 0; EditText titleField = view.findViewById(R.id.todo_title_inputField); - title = titleField.getText().toString(); NumberPicker numberPickerYear = view.findViewById(R.id.numberPickerYear); numberPickerYear.setMinValue(citrus.getCurYear()); @@ -108,7 +114,7 @@ NumberPicker numberPickerMonth = view.findViewById(R.id.numberPickerMonth); numberPickerMonth.setMinValue(1); numberPickerMonth.setMaxValue(12); - numberPickerMonth.setValue(citrus.getCurMouth()); // 月の初期値を設定 + numberPickerMonth.setValue(citrus.getCurMonth()); // 月の初期値を設定 NumberPicker numberPickerDay = view.findViewById(R.id.numberPickerDay); @@ -116,6 +122,27 @@ numberPickerDay.setMaxValue(31); numberPickerDay.setValue(citrus.getCurDay()); // 日の初期値を設定 + Spinner spinner = view.findViewById(R.id.select_todo_spinner); + bookTitles = new ArrayList<>(); + bookTitles.add("本を選択しない"); + 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()); + } + }else{ + bookTitles = new ArrayList<>(); + } + } + }); + + 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); Button cancelButton = view.findViewById(R.id.todo_cancel_button); cancelButton.setOnClickListener(v->{ @@ -124,25 +151,13 @@ Button createButton = view.findViewById(R.id.todo_create_button); createButton.setOnClickListener(v->{ - year = numberPickerYear.getValue(); // 設定年を取得 - month = numberPickerMonth.getValue(); // 設定月を取得 - day = numberPickerDay.getValue(); // 設定日を取得 - createTodo(); + int year = numberPickerYear.getValue(); // 設定年を取得 + int month = numberPickerMonth.getValue(); // 設定月を取得 + int day = numberPickerDay.getValue(); // 設定日を取得 + String title = titleField.getText().toString(); + todosViewModel.createTodo(accountId,spinner.getSelectedItemPosition(),year,month,day,title,token); ((MainActivity)getActivity()).backFragment(); }); - - Spinner spinner; - spinner = view.findViewById(R.id.select_book_spinner); - String[] books = {"book1", "book2", "book3"}; - ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, books); - adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); - spinner.setAdapter(adapter); - } - - //Todo追加時の処理 - private void createTodo() - { - todosViewModel.createTodo(citrus.getAccountId(),selectBookNum,year,month,day,title,citrus.getToken()); } } \ No newline at end of file