I'm creating an app that lets you track some League data from their api. I'm sending data to a Firestore database and pulling it to fill a RecyclerView. I'm having trouble using notifyDataSetChanged on my RecyclerView when I add or remove data to be tracked. When I swap to another fragment and back, it works, but I want it to update while you're still in the same fragment. Any help would be greatly appreciated!
Data Model:
@Parcelize data class Summoner (val summonerID: String, /* Doc ID is actually summ id */ val name: String, val pictureURL: String, val region: String) : Parcelable { companion object { fun DocumentSnapshot.toSummoner() : Summoner? { return try { val name = getString("name")!! val pictureURL = getString("icon")!! val region = getString("region")!! Summoner(id, name, pictureURL, region) } catch (e: Exception) { Log.e(TAG, "Error converting user profile", e) FirebaseCrashlytics.getInstance().log("Error converting user profile") FirebaseCrashlytics.getInstance().setCustomKey("userId", id) FirebaseCrashlytics.getInstance().recordException(e) null } } private const val TAG = "Summoner" } }
RecyclerView
class SummonerListAdapter : ListAdapter<Summoner, SummonerListAdapter.SummonerViewHolder>(SummonerComparator()) { class SummonerComparator: DiffUtil.ItemCallback<Summoner>() { override fun areItemsTheSame(oldItem: Summoner, newItem: Summoner): Boolean { return oldItem === newItem } override fun areContentsTheSame(oldItem: Summoner, newItem: Summoner): Boolean { return oldItem.name == newItem.name } } class SummonerViewHolder(private val view : View) : RecyclerView.ViewHolder(view) { val textViewName : TextView = view.findViewById(R.id.textView_followedSummName) val textViewRegion : TextView = view.findViewById(R.id.textView_followedSummRegion) val imageViewSumm : ImageView = view.findViewById(R.id.imageView_followedSumm) fun bindText(text : String?, textView: TextView) { textView.text = text } companion object { fun create (parent: ViewGroup) : SummonerViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_summoner, parent, false) return SummonerViewHolder(view) } } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SummonerViewHolder { return SummonerViewHolder.create(parent) } override fun onBindViewHolder(holder: SummonerViewHolder, position: Int) { val currentSummoner = getItem(position) holder.bindText(currentSummoner.name, holder.textViewName) holder.bindText(currentSummoner.region, holder.textViewRegion) Picasso.get().load(currentSummoner.pictureURL).into(holder.imageViewSumm) } override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { super.onAttachedToRecyclerView(recyclerView) Log.e(TAG, "attached") } }
ViewModel
class FollowedViewModel : ViewModel() { private val _summonerProfile = MutableLiveData<Summoner>() val summonerProfile : LiveData<Summoner> = _summonerProfile private val _followedSumms = MutableLiveData<List<Summoner>>() val followedSumms: LiveData<List<Summoner>> = _followedSumms init { viewModelScope.launch { _summonerProfile.value = FirebaseProfileService.getProfileData(summonerId = "") _followedSumms.value = FirebaseProfileService.getFollowedSumms(userId = "") Log.e("Followed Summs", followedSumms.toString()) } } }
Fragment Observer:
val adapter = SummonerListAdapter() val viewModel = ViewModelProvider(this)[FollowedViewModel::class.java] recyclerView = view.findViewById(R.id.recyclerView_followed) recyclerView.adapter = adapter recyclerView.layoutManager = LinearLayoutManager(context) Log.e("0", summonerList.toString()) viewModel.followedSumms.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
https://stackoverflow.com/questions/67363066/kotlin-fragment-notifydatasetchanged-in-fragment-not-working May 03, 2021 at 11:06AM
没有评论:
发表评论