Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/java/edu/temple/namelist/CustomAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomAdapter(private val names: List<String>, 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
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/edu/temple/namelist/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class MainActivity : AppCompatActivity() {

val spinner = findViewById<Spinner>(R.id.spinner)
val nameTextView = findViewById<TextView>(R.id.textView)
val deleteButton = findViewById<Button>(R.id.deleteButton)


with (spinner) {
adapter = CustomAdapter(names, this@MainActivity)
Expand All @@ -36,10 +38,23 @@ class MainActivity : AppCompatActivity() {
}
}

findViewById<View>(R.id.deleteButton).setOnClickListener {
(names as MutableList).removeAt(spinner.selectedItemPosition)
deleteButton.setOnClickListener {
// (names as MutableList).removeAt(spinner.selectedItemPosition)
// (spinner.adapter as BaseAdapter).notifyDataSetChanged()

val oldPos = spinner.selectedItemPosition
(names as MutableList).removeAt(oldPos)
(spinner.adapter as BaseAdapter).notifyDataSetChanged()
}

if (names.isNotEmpty()) {
val newPos = oldPos.coerceAtMost(names.lastIndex)
spinner.setSelection(newPos, false)
nameTextView.text = names[newPos]
}else {
nameTextView.text = ""
deleteButton.isEnabled = false

}
}
}
}