Chapter 14 Challenge: An empty view for the RecyclerView

Here is what I came up with after trying a lot.
List_item_crime.xml:

ā€¦

<TextView
    android:id="@+id/empty_crime_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:visibility="gone"
    android:text="There is no Crime Yet!"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/new_crime_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:text="New Crime"
    android:textSize="10sp"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/empty_crime_list" />
...

on CrimeListfragment:

...
        private fun updateUI(crimes: List<Crime>) {
                //val crimes = crimeListViewModel.crimes
                when (crimes.size) {
                    0    -> {
                        val adapter = EmptyCrimeAdapter()
                        crimeRecyclerView.adapter = adapter
                    }
                    else -> {
                        adapter = CrimeAdapter(crimes)
                        crimeRecyclerView.adapter = adapter
                    }
                }
        }
     private inner class CrimeHolder(view: View) : RecyclerView.ViewHolder(view),
            View.OnClickListener {
    ...
            private val emptyList: TextView = itemView.findViewById(R.id.empty_crime_list)
            private val newCrimeButton: Button = itemView.findViewById(R.id.new_crime_button)
    ...
    fun bind(crime: Crime) {...}
            fun bind() {
                Log.i(TAG, "5")
                titleTextView.visibility = View.GONE
                dateTextView.visibility = View.GONE
                solvedImageView.visibility = View.GONE
                emptyList.visibility = View.VISIBLE
                newCrimeButton.visibility = View.VISIBLE
                newCrimeButton.setOnClickListener {
                    val crime = Crime()
                    crimeListViewModel.addCrime(crime)
                    callbacks?.onCrimeSelected(crime.id)
                }
            }
    }
    private inner class EmptyCrimeAdapter : RecyclerView.Adapter<CrimeHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CrimeHolder {
                val view = layoutInflater.inflate(R.layout.list_item_crime, parent, false)
                return CrimeHolder(view)
            }
            override fun getItemCount(): Int {
                return 1
            }
            override fun onBindViewHolder(holder: CrimeHolder, position: Int) {
                holder.bind()
            }
        }

Iā€™m not sure if it was the best way to solve the problem.
I notice a problem when I click in the button and press back without setting anything, the app thinks that I added a crime with an empty title.

2 Likes

I think you can make it easier in your
UpdateUI (crimes: List) {
if (crimes.isEmpty()) {
crimeRecyclerView.visibility = View.INVISIBLE
emptyView.visibility = View.VISIBLE
}else{
crimeRecyclerView.visibility = View.VISIBLE
emptyView.visibility = View.INVISIBLE
}
}

1 Like