Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assign a unique key to each item in a LazyColumn for better performance.
Android Kotlin
LazyColumn {
items(items = itemList, key = [1]) { item ->
Text(text = item.name)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using itemList.size as key causes all items to have the same key.
Using item.name might not be unique if names repeat.
✗ Incorrect
Using a unique identifier like item.id as the key helps the LazyColumn track items efficiently and improves performance.
2fill in blank
mediumComplete the code to provide a stable key for each item in a RecyclerView adapter.
Android Kotlin
override fun getItemId(position: Int): Long {
return [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using position as ID can cause issues when items move.
Using current time causes keys to change every call.
✗ Incorrect
Returning a unique stable ID like items[position].id.toLong() helps RecyclerView optimize item animations and updates.
3fill in blank
hardFix the error in the code to correctly set stable IDs in a RecyclerView adapter.
Android Kotlin
init {
setHasStableIds([1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing false disables stable IDs, losing performance benefits.
Passing null causes a compile error.
✗ Incorrect
Calling setHasStableIds(true) tells RecyclerView that each item has a unique stable ID, enabling better performance.
4fill in blank
hardFill both blanks to create a LazyRow with items that have unique keys.
Android Kotlin
LazyRow {
items(items = [1], key = [2]) { item ->
Text(text = item.title)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using items instead of the actual list variable.
Using item.title as key may not be unique.
✗ Incorrect
Use the list variable myItemList and the unique item.id as the key for each item in LazyRow.
5fill in blank
hardFill all three blanks to create a RecyclerView adapter that uses stable IDs.
Android Kotlin
class MyAdapter(private val data: List<MyData>) : RecyclerView.Adapter<MyViewHolder>() { override fun getItemCount() = [1] override fun getItemId(position: Int): Long = [2] init { setHasStableIds([3]) } // other adapter methods... }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning position as ID instead of a unique stable ID.
Not enabling stable IDs with setHasStableIds(true).
Returning wrong item count.
✗ Incorrect
The adapter returns the size of data, uses a unique stable ID for each item, and enables stable IDs with setHasStableIds(true).