diff --git a/app/src/main/java/com/example/citrusclient/views/MyBookshelfFragment.java b/app/src/main/java/com/example/citrusclient/views/MyBookshelfFragment.java index 51865a1..7c8e285 100644 --- a/app/src/main/java/com/example/citrusclient/views/MyBookshelfFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/MyBookshelfFragment.java @@ -7,6 +7,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.lifecycle.Observer; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -20,11 +21,13 @@ import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; import com.example.citrusclient.viewmodels.BooksViewModel; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -66,7 +69,7 @@ } - private List bookList; + private List bookList; @Override public void onCreate(Bundle savedInstanceState) { @@ -81,7 +84,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_my_bookshelf_fragment, container, false); + } @@ -90,14 +95,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - bookList = createBooks(); Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getAccountId(); - BooksViewModel booksViewModel = new BooksViewModel(); - + bookList = new ArrayList<>(); + bookList.add(new Book("a", 1, "a", true, "0")); RecyclerView recyclerView = view.findViewById(R.id.my_books_list); recyclerView.setHasFixedSize(true); @@ -109,37 +113,31 @@ FloatingActionButton addButton = view.findViewById(R.id.book_add_button); addButton.setOnClickListener(v -> { -// RowBook b = new RowBook(); -// b.hogeTitle = "hoge"; -// bookList.add(b); -// bookAdapter.notifyItemInserted(bookList.size()-1); + ((MainActivity) getActivity()).showFragment(new CreateBookFragment()); }); - } + BooksViewModel booksViewModel = new BooksViewModel(); - private List createBooks(){ - List books = new ArrayList<>(); - for(int i = 0; i < 5; i++){ - RowBook book = new RowBook(); - book.hogeTitle = "hoge"; - book.color = Color.RED; - books.add(book); - } - return books; - } + booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(HashMap integerBookHashMap) { +// bookList = new ArrayList<>(integerBookHashMap.values()); +// bookAdapter.notifyDataSetChanged(); - class RowBook{ - String hogeTitle; - int color; + } + }); + booksViewModel.loadBooks("bird", "xyz"); + + } } class MyBookshelfAdapter extends RecyclerView.Adapter{ - private List bookList; + private List bookList; - MyBookshelfAdapter(List book){ + MyBookshelfAdapter(List book){ this.bookList = book; } @@ -152,9 +150,9 @@ @Override public void onBindViewHolder(@NonNull MyBookViewHolder holder, int position) { - MyBookshelfFragment.RowBook bookData = this.bookList.get(position); - holder.bookButton.setText(bookData.hogeTitle); - holder.bookButton.setBackgroundColor(bookData.color); + Book bookData = this.bookList.get(position); + holder.bookButton.setText(bookData.getTitle()); + holder.bookButton.setBackgroundColor(Color.RED); } @Override