diff --git a/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java b/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java new file mode 100644 index 0000000..2e70529 --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/views/FavoritesBooksFragment.java @@ -0,0 +1,180 @@ +package com.example.citrusclient.views; + +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import com.example.citrusclient.Citrus; +import com.example.citrusclient.R; +import com.example.citrusclient.models.Book; + +import java.util.HashMap; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link FavoritesBooksFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class FavoritesBooksFragment extends Fragment { + + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + // TODO: Rename and change types of parameters + private String mParam1; + private String mParam2; + + public FavoritesBooksFragment() { + // Required empty public constructor + } + + /** + * 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 FavoritesBooksFragment. + */ + // TODO: Rename and change types and number of parameters + public static FavoritesBooksFragment newInstance(String param1, String param2) { + FavoritesBooksFragment fragment = new FavoritesBooksFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_favorites_books, container, false); + } + + + private HashMap> bookList; + HashMap books; + HashMap integerBookHashMap; + HashMap> data; + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + Citrus citrus = (Citrus) (getActivity().getApplication()); + bookList = new HashMap<>(); + HashMap nittaBooks = new HashMap<>(); + nittaBooks.put(1, new Book("niita", 1, "ダイエット", true, "#7744FF")); + nittaBooks.put(3, new Book("niita", 3, "PHP", true, "#FF4455")); + bookList.put("nitta", nittaBooks); + + // いいねした本の一覧を返すのに必要 + RecyclerView recyclerView = view.findViewById(R.id.my_favorites_list); //いいねした本の一覧 + recyclerView.setHasFixedSize(true); //サイズの固定 + RecyclerView.LayoutManager layoutManager = new GridLayoutManager(view.getContext(), 3); //何列にするか + recyclerView.setLayoutManager(layoutManager); + FavoritesBooksAdapter favoritesBooksAdapter = new FavoritesBooksAdapter(bookList); + recyclerView.setAdapter(favoritesBooksAdapter); + + } +} + + +class FavoritesBooksAdapter extends RecyclerView.Adapter { + private HashMap> favoritesBooksList; + //private HashMap integerBookHashMap; + //private HashMap> data; + + FavoritesBooksAdapter(HashMap> favoritesBooksList) { //初期化 + this.favoritesBooksList = favoritesBooksList; + // this.data = data; + // this.integerBookHashMap = new HashMap<>(); + } + + public void setFavoritesBooks(HashMap> favoritesBooksList) { + this.favoritesBooksList = favoritesBooksList; +// if(integerBookHashMap != null) { +// this.integerBookHashMap = new HashMap<>(integerBookHashMap); +// } else { +// this.integerBookHashMap = new HashMap<>(); +// } + notifyDataSetChanged(); + } + + + @NonNull + @Override + public FavoritesBooksViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_favorites_book, parent, false); + return new FavoritesBooksViewHolder(view); + } + @Override + public void onBindViewHolder(@NonNull FavoritesBooksViewHolder holder, int position) { //表示に関係する +// book = favoritesBooksList.get(position); +// holder.favorites_book.setText(book.getTitle()); +// +// TextView other = (TextView) view.findViewById(R.id.other_id); +// +// holder.favorites_book.setText(book.getTitle()); + + int count = 0; + for (String otherAccountId: favoritesBooksList.keySet()) { + HashMap bookMap = favoritesBooksList.get(otherAccountId); + for (Integer bookId : bookMap.keySet()) { + Book book = bookMap.get(bookId); + if (count == position){ + holder.other_id.setText(otherAccountId); + holder.favorites_book.setText(book.getTitle()); + int parsedColor = Color.parseColor(book.getColor()); + holder.favorites_book.setBackgroundTintList(ColorStateList.valueOf(parsedColor)); + return; + } + count++; + + } + + } + } + + + static class FavoritesBooksViewHolder extends RecyclerView.ViewHolder { + TextView other_id; + Button favorites_book; + + public FavoritesBooksViewHolder(@NonNull View itemView) { + super(itemView); + favorites_book = itemView.findViewById(R.id.favorites_book); + other_id = itemView.findViewById(R.id.other_id); + } + } + + @Override + public int getItemCount() { + return favoritesBooksList.size(); + } // これがないとclassにエラー + +}