I have a home page containing a RecyclerView
as well as an "Add Show" button which redirects me to a form in order to add a show to the recyclerView.
My goal is, when the page to add a show closes, to have the newly added show displayed in my recyclerView. However, that only happens about half the times, which obviously isn't what I want. I've tried using a ValueEventListener
in my adapter, but besides looking like a bad idea, it doesn't even work.
I've also tried using notifyItemInserted
but I don't know how I should implement it, as my AddShowActivity
doesn't have any link with the adapter. So right now I don't have any idea left and that's why I'm here.
Both my AddShowActivity
and my adapter are called from my HomeFragment
:
class HomeFragment(private val context: MainActivity) : Fragment() {
private lateinit var verticalRecyclerView: RecyclerView
private lateinit var buttonAddShow: Button
private lateinit var showsAdapter: ShowAdapter
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_home, container, false)
setupComponents(view)
initializeComponents()
return view
}
private fun setupComponents(view: View) {
verticalRecyclerView = view.findViewById(R.id.home_recycler_view)
buttonAddShow = view.findViewById(R.id.home_button_add_show)
showsAdapter = ShowAdapter(context, R.layout.item_show, null)
}
private fun initializeComponents() {
verticalRecyclerView.adapter = showsAdapter
swipeRefreshLayout.setOnRefreshListener{
refreshFragment()
}
buttonAddShow.setOnClickListener{
startActivity(Intent(context, AddShowActivity::class.java))
}
}
}
CodePudding user response:
the fast way is that.
- assign the adapter to a public static var and you need to check if the var value is available or not before using
- once any underlay data in the adapter is changed. call the adapter notifyDataSetChanged method to refresh the UI
CodePudding user response:
There are mutiple ways:
- If you are willing to change AddShowActivity to Fragment, then you can use MVVM which contains your
ShowList
wrapped with an Observer pattern (ex: LiveData). When your list changes (for example: AddShow), the Adapter will be notified abt that and refresh itself:
viewModel.showList.observe(viewLifecycle) {
showsAdapter.setData(it)
notifyDataSetChanged()
}
On the other hand, if you not prefer MVVM, you can also register a FragmentResultListener
, then manually update the RV here.
- If there are 2 seperate Activities, I just wonder does it always fetch data each time Activity start/restart, so you kinda don't have to care about that?
CodePudding user response:
Basically, you want your list of shows to refresh when you add a new show.
You can do that in the Fragment class by adding notifyDataSetChanged
after you add new data to the list. However, Android studio gives a warning about how expensive the operation becomes.
An easier solution to notifyDataSetChanged
is using a DiffUtil
. notifyDataSetChanged
refreshes the whole list even if 99% of the list items remain unchanged which can cause a decrease in user experience as the list increases. DiffUtil
fixes this by checking for items that have changed and updating only those items.