comparison android/DWindows.kt @ 2753:2ac361d3a837

Android: Rewrite listbox multiple selection... keep track of the selection ourselves using a "multiple" list in our DWListBox class. Show that an item is selected by changing its background to dark gray. I try to do the "native" thing on platforms when possible, but the Android native checked item wasn't really working. Can revisit later if we want a more native version.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 01 Jan 2022 18:47:38 +0000
parents d355c6abbba6
children e256bd8628ba
comparison
equal deleted inserted replaced
2752:d355c6abbba6 2753:2ac361d3a837
1195 ) 1195 )
1196 } 1196 }
1197 1197
1198 class DWListBox(context: Context) : ListView(context), OnItemClickListener { 1198 class DWListBox(context: Context) : ListView(context), OnItemClickListener {
1199 var list = mutableListOf<String>() 1199 var list = mutableListOf<String>()
1200 var multiple = mutableListOf<Int>()
1200 var selected: Int = -1 1201 var selected: Int = -1
1201 var colorFore: Int? = null 1202 var colorFore: Int? = null
1202 var colorBack: Int? = null 1203 var colorBack: Int? = null
1204 var colorSelected: Int = Color.DKGRAY
1203 1205
1204 init { 1206 init {
1205 setAdapter( 1207 adapter = object : ArrayAdapter<String>(
1206 object : ArrayAdapter<String>(
1207 context, 1208 context,
1208 R.layout.simple_list_item_1, list 1209 R.layout.simple_list_item_1, list
1209 ) { 1210 ) {
1210 override fun getView(pos: Int, view: View?, parent: ViewGroup): View { 1211 override fun getView(pos: Int, view: View?, parent: ViewGroup): View {
1211 val thisview = super.getView(pos, view, parent) 1212 val thisview = super.getView(pos, view, parent)
1212 val textview = thisview as TextView 1213 val textview = thisview as TextView
1213 if (colorFore != null) { 1214 if (colorFore != null) {
1214 textview.setTextColor(colorFore!!) 1215 textview.setTextColor(colorFore!!)
1215 } 1216 }
1216 if (colorBack != null) { 1217 if (colorBack != null) {
1217 textview.setBackgroundColor(colorBack!!) 1218 textview.setBackgroundColor(colorBack!!)
1218 } 1219 }
1219 return thisview 1220 return thisview
1220 } 1221 }
1221 } 1222 }
1222 )
1223 onItemClickListener = this 1223 onItemClickListener = this
1224 } 1224 }
1225 1225
1226 override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) { 1226 override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
1227 selected = position 1227 selected = position
1228 if(this.choiceMode == ListView.CHOICE_MODE_MULTIPLE) {
1229 if(multiple.contains(position)) {
1230 multiple.remove(position)
1231 view.setBackgroundColor(Color.TRANSPARENT)
1232 } else {
1233 multiple.add(position)
1234 view.setBackgroundColor(colorSelected)
1235 }
1236 }
1228 eventHandlerInt(DWEvent.LIST_SELECT, position, 0, 0, 0) 1237 eventHandlerInt(DWEvent.LIST_SELECT, position, 0, 0, 0)
1229 } 1238 }
1230 1239
1231 external fun eventHandlerInt( 1240 external fun eventHandlerInt(
1232 message: Int, 1241 message: Int,
4390 combobox.list.add(text) 4399 combobox.list.add(text)
4391 } else if(window is DWListBox) { 4400 } else if(window is DWListBox) {
4392 val listbox = window 4401 val listbox = window
4393 4402
4394 listbox.list.add(text) 4403 listbox.list.add(text)
4404 listbox.multiple.clear()
4395 val adapter = listbox.adapter as ArrayAdapter<String> 4405 val adapter = listbox.adapter as ArrayAdapter<String>
4396 adapter.notifyDataSetChanged() 4406 adapter.notifyDataSetChanged()
4397 } 4407 }
4398 } 4408 }
4399 } 4409 }
4407 combobox.list.add(pos, text) 4417 combobox.list.add(pos, text)
4408 } else if(window is DWListBox) { 4418 } else if(window is DWListBox) {
4409 val listbox = window 4419 val listbox = window
4410 4420
4411 listbox.list.add(pos, text) 4421 listbox.list.add(pos, text)
4422 listbox.multiple.clear()
4412 val adapter = listbox.adapter as ArrayAdapter<String> 4423 val adapter = listbox.adapter as ArrayAdapter<String>
4413 adapter.notifyDataSetChanged() 4424 adapter.notifyDataSetChanged()
4414 } 4425 }
4415 } 4426 }
4416 } 4427 }
4424 combobox.list.clear() 4435 combobox.list.clear()
4425 } else if(window is DWListBox) { 4436 } else if(window is DWListBox) {
4426 val listbox = window 4437 val listbox = window
4427 4438
4428 listbox.list.clear() 4439 listbox.list.clear()
4440 listbox.multiple.clear()
4429 val adapter = listbox.adapter as ArrayAdapter<String> 4441 val adapter = listbox.adapter as ArrayAdapter<String>
4430 adapter.notifyDataSetChanged() 4442 adapter.notifyDataSetChanged()
4431 } 4443 }
4432 } 4444 }
4433 } 4445 }
4545 } else if(window is DWListBox) { 4557 } else if(window is DWListBox) {
4546 val listbox = window 4558 val listbox = window
4547 4559
4548 if(index < listbox.list.count()) { 4560 if(index < listbox.list.count()) {
4549 listbox.list.removeAt(index) 4561 listbox.list.removeAt(index)
4562 listbox.multiple.clear()
4550 val adapter = listbox.adapter as ArrayAdapter<String> 4563 val adapter = listbox.adapter as ArrayAdapter<String>
4551 adapter.notifyDataSetChanged() 4564 adapter.notifyDataSetChanged()
4552 } 4565 }
4553 } 4566 }
4554 } 4567 }
4572 var retval: Int = -1 4585 var retval: Int = -1
4573 4586
4574 waitOnUiThread { 4587 waitOnUiThread {
4575 if(window is DWListBox) { 4588 if(window is DWListBox) {
4576 val listbox = window 4589 val listbox = window
4577 val checked: SparseBooleanArray = listbox.getCheckedItemPositions()
4578 4590
4579 // If we are starting over.... 4591 // If we are starting over....
4580 if(where == -1 && checked.size() > 0) { 4592 if(where == -1 && listbox.multiple.count() > 0) {
4581 retval = checked.keyAt(0) 4593 retval = listbox.multiple[0]
4582 } else { 4594 } else {
4583 // Otherwise loop until we find our current place 4595 // Otherwise loop until we find our current place
4584 for (i in 0 until checked.size()) { 4596 for (i in 0 until listbox.multiple.count()) {
4585 // Item position in adapter 4597 // Item position in adapter
4586 val position: Int = checked.keyAt(i) 4598 val position: Int = listbox.multiple[i]
4587 // If we are at our current point... check to see 4599 // If we are at our current point... check to see
4588 // if there is another one, and return it... 4600 // if there is another one, and return it...
4589 // otherwise we will return -1 to indicated we are done. 4601 // otherwise we will return -1 to indicated we are done.
4590 if (where == position && (i+1) < checked.size()) { 4602 if (where == position && (i+1) < listbox.multiple.count()) {
4591 retval = checked.keyAt(i+1) 4603 retval = listbox.multiple[i+1]
4592 } 4604 }
4593 } 4605 }
4594 } 4606 }
4595 } 4607 }
4596 } 4608 }