diff --git a/app/src/main/java/com/example/citrusclient/views/MyAdapter.java b/app/src/main/java/com/example/citrusclient/views/MyAdapter.java index 6b72b78..ae0198f 100644 --- a/app/src/main/java/com/example/citrusclient/views/MyAdapter.java +++ b/app/src/main/java/com/example/citrusclient/views/MyAdapter.java @@ -17,6 +17,9 @@ import com.example.citrusclient.viewmodels.FavoritesViewModel; import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import androidx.annotation.NonNull; @@ -27,6 +30,7 @@ public class MyAdapter extends RecyclerView.Adapter { private List originalList; // 元のデータリスト private List filteredList; // フィルタリングされたリスト + private HashMap> favoritesMap = new HashMap<>(); public MyAdapter(List originalList) { @@ -46,6 +50,9 @@ Book currentBook = filteredList.get(position); holder.bind(currentBook); // bindメソッドで設定 Context context = holder.itemView.getContext(); + Activity activity = (Activity) context; + Citrus citrus = (Citrus) activity.getApplication(); + FavoritesViewModel favoritesViewModel = new ViewModelProvider((FragmentActivity)context).get(FavoritesViewModel.class); // 背景色の設定 int red, green, blue; @@ -59,16 +66,28 @@ holder.titleTextView.setBackgroundColor(Color.rgb(red, green, blue)); holder.titleTextView.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); holder.titleTextView.setOnClickListener(v -> { - Activity activity = (Activity) context; - Citrus citrus = (Citrus) activity.getApplication(); citrus.setCurLookingAccountId(currentBook.getAccountId()); citrus.setCurLookingBookId(currentBook.getBookId()); + Calendar calendar = Calendar.getInstance(); + citrus.setCurYear(calendar.get(Calendar.YEAR)); + citrus.setCurMonth(calendar.get(Calendar.MONTH) + 1); + citrus.setCurDay(calendar.get(Calendar.DATE)); ((MainActivity) activity).showFragment(new OtherHomeFragment()); }); +// if (favoritesMap != null && favoritesMap.containsKey(currentBook.getAccountId())) { +// if (favoritesMap.get(currentBook.getAccountId()).contains(currentBook.getBookId())) { +// holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_24); +// holder.LikeTextView.setTag("liked"); +// } else { +// holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); +// holder.LikeTextView.setTag("unliked"); +// } +// } else { +// holder.LikeTextView.setImageResource(R.drawable.baseline_favorite_border_24); +// holder.LikeTextView.setTag("unliked"); +// } + holder.LikeTextView.setOnClickListener(view -> { - Activity activity = (Activity) context; - Citrus citrus = (Citrus) activity.getApplication(); - FavoritesViewModel favoritesViewModel = new ViewModelProvider((FragmentActivity)context).get(FavoritesViewModel.class); System.out.println(holder.LikeTextView.getTag()); if(holder.LikeTextView.getTag().equals("unliked")){ System.out.println(citrus.getAccountId()+"が"+currentBook.getAccountId()+"の"+currentBook.getBookId()+"を登録しました"); @@ -115,6 +134,10 @@ } notifyDataSetChanged(); } + public void updateFavorites(HashMap> newFavorites){ + this.favoritesMap = newFavorites; + notifyDataSetChanged(); + } static class ViewHolder extends RecyclerView.ViewHolder { TextView titleTextView;