북마크 페이지 관련 질문 있습니다
북마크 페이지에서 북마크가 해지되긴 하는데, 같이 북마크 등록되어있는 요소들이 한 번 더 나옵니다. 제가 수정한 BookmarkRVAdapter 와 BookmarkFragment 코드 첨부하겠습니다. BookmarkRVAdapter package com.example.mysololife.contentsList import android.content.Context import android.content.Intent import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import android.widget.Toast import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.example.mysololife.R import com.example.mysololife.utils.FBAuth import com.example.mysololife.utils.FBRef class BookmarkRVAdapter(val context : Context, val items:ArrayList, val keyList : ArrayList, val bookmarkIdList : MutableList) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookmarkRVAdapter.Viewholder { val v = LayoutInflater.from(parent.context).inflate(R.layout.content_rv_item, parent, false) Log.d("BookmarkRVAdapter", keyList.toString()) Log.d("BookmarkRVAdapter", bookmarkIdList.toString()) return Viewholder(v) } override fun onBindViewHolder(holder: BookmarkRVAdapter.Viewholder, position: Int) { holder.bindItems(items[position], keyList[position]) } override fun getItemCount(): Int { return items.size } inner class Viewholder(itemView : View) : RecyclerView.ViewHolder(itemView){ fun bindItems(item: ContentModel, key: String){ itemView.setOnClickListener{ Toast.makeText(context, item.title, Toast.LENGTH_LONG).show() val intent = Intent(context, ContentShowActivity::class.java) intent.putExtra("url", item.webUrl) itemView.context.startActivity(intent) } val contentTitle= itemView.findViewById(R.id.textArea) val imageViewArea = itemView.findViewById(R.id.imageArea) val bookmarkArea = itemView.findViewById(R.id.bookmarkArea) if(bookmarkIdList.contains(key)){ bookmarkArea.setImageResource(R.drawable.bookmark_color) } else { bookmarkArea.setImageResource(R.drawable.bookmark_white) } bookmarkArea.setOnClickListener { Log.d("BookmarkRVAdapter", FBAuth.getUid()) Toast.makeText(context, key, Toast.LENGTH_LONG).show() //북마크 있을때 if(bookmarkIdList.contains(key)){ bookmarkIdList.clear() Log.d("BookmarkRVAdapter", keyList.toString()) Log.d("BookmarkRVAdapter", bookmarkIdList.toString()) FBRef.bookmarkRef .child(FBAuth.getUid()) .child(key) .removeValue() } else{ FBRef.bookmarkRef .child(FBAuth.getUid()) .child(key) .setValue(BookmarkModel(true)) } //북마크 없을 때 } contentTitle.text = item.title Glide.with(context).load(item.imageUrl).into(imageViewArea) } } } BookmarkFragment package com.example.mysololife.fragments import android.os.Bundle import android.util.Log import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.databinding.DataBindingUtil import androidx.navigation.findNavController import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.mysololife.R import com.example.mysololife.contentsList.BookmarkRVAdapter import com.example.mysololife.contentsList.ContentModel import com.example.mysololife.databinding.FragmentBookmarkBinding import com.example.mysololife.utils.FBAuth import com.example.mysololife.utils.FBRef import com.google.firebase.database.DataSnapshot import com.google.firebase.database.DatabaseError import com.google.firebase.database.ValueEventListener class BookmarkFragment : Fragment() { private lateinit var binding:FragmentBookmarkBinding private var TAG = BookmarkFragment::class.java val bookmarkIdList = mutableListOf() val items = ArrayList() val itemKeyList = ArrayList() lateinit var rvAdapter : BookmarkRVAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { binding = DataBindingUtil.inflate(inflater, R.layout.fragment_bookmark, container, false) //2. 사용자가 북마크한 정보를 다 가져옴 getBookmarkData() rvAdapter= BookmarkRVAdapter(requireContext(), items, itemKeyList, bookmarkIdList) val rv : RecyclerView = binding.bookmarkRV rv.adapter = rvAdapter rv.layoutManager = GridLayoutManager(requireContext(), 2) binding.homeTap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_homeFragment) } binding.tipTap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_tipFragment) } binding.talkTap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_talkFragment) } binding.storeTap.setOnClickListener { it.findNavController().navigate(R.id.action_bookmarkFragment_to_storeFragment) } // Inflate the layout for this fragment return binding.root } private fun getCategoryData(){ val postListener = object : ValueEventListener { override fun onDataChange(dataSnapshot: DataSnapshot) { for (dataModel in dataSnapshot.children){ val item = dataModel.getValue(ContentModel::class.java) //3. 전체 컨텐츠 중, 사용자가 북마크한 정보만 보여줌 if(bookmarkIdList.contains(dataModel.key.toString())){ items.add(item!!) itemKeyList.add(dataModel.key.toString()) } } rvAdapter.notifyDataSetChanged() } override fun onCancelled(databaseError: DatabaseError) { Log.w("ContentListActivity", "loadPost:onCancelled", databaseError.toException()) } } FBRef.category1.addValueEventListener(postListener) FBRef.category2.addValueEventListener(postListener) } private fun getBookmarkData(){ val postListener = object : ValueEventListener { override fun onDataChange(dataSnapshot: DataSnapshot) { bookmarkIdList.clear() for (dataModel in dataSnapshot.children){ bookmarkIdList.add(dataModel.key.toString()) } //1. 전체 카테고리에 있는 컨텐츠 데이터들을 다 가져옴 getCategoryData() } override fun onCancelled(databaseError: DatabaseError) { Log.w("ContentListActivity", "loadPost:onCancelled", databaseError.toException()) } } FBRef.bookmarkRef.child(FBAuth.getUid()).addValueEventListener(postListener) } } 현재 북마크 등록 상황 (사진) => title1의 북마크 부분을 눌렀을 경우 (사진)