I have a problem with deleting crime challenge in chap15

I tried to do the challenge in CrimeDetailFragment, it works fine but when there is only one crime available in recyclerView, i need to delete it twice, addmenu() is used in onViewCreated

  private fun addMenu() {
        (requireActivity() as MenuHost).addMenuProvider(object : MenuProvider {
            override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
                menuInflater.inflate(R.menu.fragment_crime_detail, menu)
            }

            override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
                return when (menuItem.itemId) {
                    R.id.delete_crime -> {
                        viewLifecycleOwner.lifecycleScope.launch {
                            crimeDetailViewModel.crime.value?.let { crime ->
                                crimeDetailViewModel.deleteCrime(
                                    crime = crime
                                )
                            }
                            binding.root.visibility = View.GONE
                            findNavController().popBackStack()
                        }
                        true
                    }
                    else -> false
                }
            }

        }, viewLifecycleOwner, Lifecycle.State.STARTED)
    }

and another weird thing is that when there is only one crime , the first time i delete it , and press it again , crime title is empty

1 Like

I’m not sure I can help you much without seeing the bigger picture. Is there any way that you could share what you have via Github or something similar?

thank you for answering, I kind of solve the problem by adding a line of code to change recyclerView’s visibility to GONE when there are no crimes , but i still wonder what is the reason for this problem

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewLifecycleOwner.lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED)
            {
                crimeLitViewModel.crimes.collect { crimes ->
                    if (crimes.isEmpty()) {
                        binding.crimeRecyclerView.visibility = View.GONE
                        binding.crimeListEmptyWarning.visibility = View.VISIBLE
                        binding.crimeListEmptyWarning.setOnClickListener {
                            showCrime()
                        }
                    } else {
                        binding.crimeListEmptyWarning.visibility = View.GONE
                        binding.crimeRecyclerView.adapter = CrimeListAdapter(crimes) { crimeId ->
                            findNavController()
                                .navigate(CrimeListFragmentDirections.showCrimeDetail(crimeId))
                        }
                    }

                }
            }
        }

and here is the Github link : https://github.com/ali72313/bignerdRanch-5thedition

I tried running your code and it seems all good on my end. I’m not exactly sure what is causing the issues, because it looks like you took the right approach to solve the challenge.

When we create a new crime, the title starts off as empty. Maybe that is what you were seeing.

hi again, and thank you for answering, if i remove this line of code :

binding.crimeRecyclerView.visibility = View.GONE

in CrimeListFragment i get the same weird result , i kinna don’t feel good about this solution, cause i don’t know the source of problem

Yeah, I understand not feeling great when you don’t know what is going wrong. Changing the visibility on the RecyclerView doesn’t solve the problem, it just hides it.

If you still cannot find what is causing issues, I recommend adding some logging inside the crimeLitViewModel.crimes.collect { ... } lambda expression. That should get invoked every time the data in the database changes, so if you are not seeing logs often, then you know that might be a place to dig into.