본문 바로가기
IT/Android

Kotlin] 안드로이드 리스트뷰 삭제 (다중선택)

by 깻잎쌈 2020. 3. 18.
반응형
// 아이템 배열 
val items = ArrayList<String>()
// 어댑터 생성 및 설정 
val adapter by lazy {  ArrayAdapter(this, android.R.layout.imple_list_item_multiple_choice, items) }
listView.adapter = adapter
listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE

...

// 선택된 아이템들 삭제버튼 
 deleteListButton.setOnClickListener {
 
       val checkedItems = listView.checkedItemPositions
       for (i in adapter.count - 1 downTo 0) {
           if (checkedItems.get(i)) {
               Log.d("선택된 아이템 삭제", i.toString())
               items.removeAt(i)
            }
        }
        
       adapter.notifyDataSetChanged()

       // 선택 초기화
       listView.clearChoices()
}
반응형

댓글