From 1de4462998a118bb08476d1154299740e6850cca Mon Sep 17 00:00:00 2001 From: Kivanc Peltek Date: Wed, 18 Feb 2026 16:18:38 -0500 Subject: [PATCH] Fix crashes caused by delete button Bug 1: CustomAdapter.getCount() was hardcoded to return 5 instead of the actual list size. After deleting a name, the adapter still reported 5 items, causing an IndexOutOfBoundsException. Fixed by returning names.size. Bug 2: The delete button had no guard for an empty list. Pressing delete after all names were removed caused a crash. Fixed by checking names.isNotEmpty() before removing, and clearing the text view when the list becomes empty. --- app/src/main/java/edu/temple/namelist/CustomAdapter.kt | 2 +- app/src/main/java/edu/temple/namelist/MainActivity.kt | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/edu/temple/namelist/CustomAdapter.kt b/app/src/main/java/edu/temple/namelist/CustomAdapter.kt index 0288b82..267216c 100644 --- a/app/src/main/java/edu/temple/namelist/CustomAdapter.kt +++ b/app/src/main/java/edu/temple/namelist/CustomAdapter.kt @@ -10,7 +10,7 @@ class CustomAdapter(private val names: List, private val context: Contex // How many items are in the collection override fun getCount(): Int { - return 5 + return names.size } // Fetch an item from the collection diff --git a/app/src/main/java/edu/temple/namelist/MainActivity.kt b/app/src/main/java/edu/temple/namelist/MainActivity.kt index f3a49f5..6e93014 100644 --- a/app/src/main/java/edu/temple/namelist/MainActivity.kt +++ b/app/src/main/java/edu/temple/namelist/MainActivity.kt @@ -37,8 +37,13 @@ class MainActivity : AppCompatActivity() { } findViewById(R.id.deleteButton).setOnClickListener { - (names as MutableList).removeAt(spinner.selectedItemPosition) - (spinner.adapter as BaseAdapter).notifyDataSetChanged() + if (names.isNotEmpty()) { + (names as MutableList).removeAt(spinner.selectedItemPosition) + (spinner.adapter as BaseAdapter).notifyDataSetChanged() + if (names.isEmpty()) { + nameTextView.text = "" + } + } } }