changeset 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
files android/DWindows.kt
diffstat 1 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Fri Dec 31 23:21:44 2021 +0000
+++ b/android/DWindows.kt	Sat Jan 01 18:47:38 2022 +0000
@@ -1197,34 +1197,43 @@
 
 class DWListBox(context: Context) : ListView(context), OnItemClickListener {
     var list = mutableListOf<String>()
+    var multiple = mutableListOf<Int>()
     var selected: Int = -1
     var colorFore: Int? = null
     var colorBack: Int? = null
+    var colorSelected: Int = Color.DKGRAY
 
     init {
-        setAdapter(
-            object : ArrayAdapter<String>(
+        adapter = object : ArrayAdapter<String>(
                 context,
                 R.layout.simple_list_item_1, list
-            ) {
-                override fun getView(pos: Int, view: View?, parent: ViewGroup): View {
-                    val thisview = super.getView(pos, view, parent)
-                    val textview = thisview as TextView
-                    if (colorFore != null) {
-                        textview.setTextColor(colorFore!!)
-                    }
-                    if (colorBack != null) {
-                        textview.setBackgroundColor(colorBack!!)
-                    }
-                    return thisview
+        ) {
+            override fun getView(pos: Int, view: View?, parent: ViewGroup): View {
+                val thisview = super.getView(pos, view, parent)
+                val textview = thisview as TextView
+                if (colorFore != null) {
+                    textview.setTextColor(colorFore!!)
                 }
-            }
-        )
+                if (colorBack != null) {
+                    textview.setBackgroundColor(colorBack!!)
+                }
+                return thisview
+            }
+        }
         onItemClickListener = this
     }
 
     override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
         selected = position
+        if(this.choiceMode == ListView.CHOICE_MODE_MULTIPLE) {
+            if(multiple.contains(position)) {
+                multiple.remove(position)
+                view.setBackgroundColor(Color.TRANSPARENT)
+            } else {
+                multiple.add(position)
+                view.setBackgroundColor(colorSelected)
+            }
+        }
         eventHandlerInt(DWEvent.LIST_SELECT, position, 0, 0, 0)
     }
 
@@ -4392,6 +4401,7 @@
                 val listbox = window
 
                 listbox.list.add(text)
+                listbox.multiple.clear()
                 val adapter = listbox.adapter as ArrayAdapter<String>
                 adapter.notifyDataSetChanged()
             }
@@ -4409,6 +4419,7 @@
                 val listbox = window
 
                 listbox.list.add(pos, text)
+                listbox.multiple.clear()
                 val adapter = listbox.adapter as ArrayAdapter<String>
                 adapter.notifyDataSetChanged()
             }
@@ -4426,6 +4437,7 @@
                 val listbox = window
 
                 listbox.list.clear()
+                listbox.multiple.clear()
                 val adapter = listbox.adapter as ArrayAdapter<String>
                 adapter.notifyDataSetChanged()
             }
@@ -4547,6 +4559,7 @@
 
                 if(index < listbox.list.count()) {
                     listbox.list.removeAt(index)
+                    listbox.multiple.clear()
                     val adapter = listbox.adapter as ArrayAdapter<String>
                     adapter.notifyDataSetChanged()
                 }
@@ -4574,21 +4587,20 @@
         waitOnUiThread {
             if(window is DWListBox) {
                 val listbox = window
-                val checked: SparseBooleanArray = listbox.getCheckedItemPositions()
 
                 // If we are starting over....
-                if(where == -1 && checked.size() > 0) {
-                    retval = checked.keyAt(0)
+                if(where == -1 && listbox.multiple.count() > 0) {
+                    retval = listbox.multiple[0]
                 } else {
                     // Otherwise loop until we find our current place
-                    for (i in 0 until checked.size()) {
+                    for (i in 0 until listbox.multiple.count()) {
                         // Item position in adapter
-                        val position: Int = checked.keyAt(i)
+                        val position: Int = listbox.multiple[i]
                         // If we are at our current point... check to see
                         // if there is another one, and return it...
                         // otherwise we will return -1 to indicated we are done.
-                        if (where == position && (i+1) < checked.size()) {
-                            retval = checked.keyAt(i+1)
+                        if (where == position && (i+1) < listbox.multiple.count()) {
+                            retval = listbox.multiple[i+1]
                         }
                     }
                 }