changeset 2754:e256bd8628ba

Andrdoi: Attempt to fix a number of issues querying containers. Always returning nullptr from the query functions made it always fail. Varaibles were incorrectly named and I had incorrectly assumed the SparseBooleanArray only returned checked items. This is not the case, it only returns items that were changed. So we now take this array and create a copy but only including the items that are checked so the code works as intended.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 02 Jan 2022 19:06:04 +0000
parents 2ac361d3a837
children 9477910393ac
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 44 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Sat Jan 01 18:47:38 2022 +0000
+++ b/android/DWindows.kt	Sun Jan 02 19:06:04 2022 +0000
@@ -1663,9 +1663,17 @@
     constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
     constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {}
 
+    fun updateBackground() {
+        if(mChecked) {
+            this.setBackgroundColor(Color.DKGRAY)
+        } else {
+            this.setBackgroundColor(Color.TRANSPARENT)
+        }
+    }
+
     override fun setChecked(b: Boolean) {
         mChecked = b
-        refreshDrawableState()
+        updateBackground()
     }
 
     override fun isChecked(): Boolean {
@@ -1674,19 +1682,7 @@
 
     override fun toggle() {
         mChecked = !mChecked
-        refreshDrawableState()
-    }
-
-    override fun onCreateDrawableState(extraSpace: Int): IntArray {
-        val states = super.onCreateDrawableState(extraSpace + 1)
-        if (mChecked) {
-            mergeDrawableStates(states, CHECKED_STATE)
-        }
-        return states
-    }
-
-    companion object {
-        val CHECKED_STATE = intArrayOf(R.attr.state_checked)
+        updateBackground()
     }
 }
 
@@ -4009,7 +4005,7 @@
                 val rowView: DWContainerRow = view as DWContainerRow
 
                 view.isSelected = !view.isSelected
-                rowView.isChecked = !rowView.isChecked
+                rowView.toggle()
                 adapter.selectedItem = position
                 lastClickView = cont!!
                 // If we are single select or we got a double tap...
@@ -4071,6 +4067,19 @@
         }
     }
 
+    // Create a new SparseBooleanArray with only the true or false contents
+    private fun onlyBooleanArray(array: SparseBooleanArray, bool: Boolean): SparseBooleanArray
+    {
+        val newArray = SparseBooleanArray()
+
+        for (i in 0 until array.size()) {
+            if (array.valueAt(i) == bool) {
+                newArray.put(array.keyAt(i), bool)
+            }
+        }
+        return newArray
+    }
+
     fun containerGetTitleStart(cont: ListView, flags: Int): String?
     {
         var retval: String? = null
@@ -4080,7 +4089,7 @@
 
             // Handle DW_CRA_SELECTED
             if((flags and 1) != 0) {
-                val checked: SparseBooleanArray = cont.getCheckedItemPositions()
+                val checked: SparseBooleanArray = onlyBooleanArray(cont.checkedItemPositions, true)
                 val position = checked.keyAt(0)
 
                 adapter.model.querypos = position
@@ -4107,7 +4116,7 @@
             if(adapter.model.querypos != -1) {
                 // Handle DW_CRA_SELECTED
                 if ((flags and 1) != 0) {
-                    val checked: SparseBooleanArray = cont.getCheckedItemPositions()
+                    val checked: SparseBooleanArray = onlyBooleanArray(cont.checkedItemPositions, true)
 
                     // Otherwise loop until we find our current place
                     for (i in 0 until checked.size()) {
@@ -4143,7 +4152,7 @@
 
             // Handle DW_CRA_SELECTED
             if((flags and 1) != 0) {
-                val checked: SparseBooleanArray = cont.getCheckedItemPositions()
+                val checked: SparseBooleanArray = onlyBooleanArray(cont.checkedItemPositions, true)
                 val position = checked.keyAt(0)
 
                 adapter.model.querypos = position
@@ -4170,7 +4179,7 @@
             if(adapter.model.querypos != -1) {
                 // Handle DW_CRA_SELECTED
                 if ((flags and 1) != 0) {
-                    val checked: SparseBooleanArray = cont.getCheckedItemPositions()
+                    val checked: SparseBooleanArray = onlyBooleanArray(cont.checkedItemPositions, true)
 
                     // Otherwise loop until we find our current place
                     for (i in 0 until checked.size()) {
--- a/android/dw.cpp	Sat Jan 01 18:47:38 2022 +0000
+++ b/android/dw.cpp	Sun Jan 02 19:06:04 2022 +0000
@@ -4028,22 +4028,24 @@
             jmethodID containerGetDataStart = env->GetMethodID(clazz, "containerGetDataStart",
                                                                "(Landroid/widget/ListView;I)J");
             // Call the method on the object
-            jlong data = env->CallLongMethod(_dw_obj, containerGetDataStart, handle, (int)flags);
+            jlong data = env->CallLongMethod(_dw_obj, containerGetDataStart, handle, (jint)flags);
             if(!_dw_jni_check_exception(env))
                 retval = (char *)data;
         } else {
             // First get the class that contains the method you need to call
             jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
             // Get the method that you want to call
-            jmethodID containerGetDataStart = env->GetMethodID(clazz, "containerGetTitleStart",
+            jmethodID containerGetTitleStart = env->GetMethodID(clazz, "containerGetTitleStart",
                                                                "(Landroid/widget/ListView;I)Ljava/lang/String;");
             // Call the method on the object
-            jstring jstr = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, containerGetDataStart, handle, (int)flags), _DW_REFERENCE_NONE);
-            if(jstr)
-                retval = (char *)env->GetStringUTFChars(jstr, nullptr);
+            jstring jstr = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, containerGetTitleStart, handle, (jint)flags), _DW_REFERENCE_NONE);
+            char *str;
+
+            if(jstr && (str = (char *)env->GetStringUTFChars(jstr, nullptr)))
+                retval = strdup(str);
         }
     }
-    return nullptr;
+    return retval;
 }
 
 /*
@@ -4070,22 +4072,24 @@
             jmethodID containerGetDataStart = env->GetMethodID(clazz, "containerGetDataNext",
                                                                "(Landroid/widget/ListView;I)J");
             // Call the method on the object
-            jlong data = env->CallLongMethod(_dw_obj, containerGetDataStart, handle, (int)flags);
+            jlong data = env->CallLongMethod(_dw_obj, containerGetDataStart, handle, (jint)flags);
             if(!_dw_jni_check_exception(env))
                 retval = (char *)data;
         } else {
             // First get the class that contains the method you need to call
             jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
             // Get the method that you want to call
-            jmethodID containerGetDataStart = env->GetMethodID(clazz, "containerGetTitleNext",
+            jmethodID containerGetTitleNext = env->GetMethodID(clazz, "containerGetTitleNext",
                                                                "(Landroid/widget/ListView;I)Ljava/lang/String;");
             // Call the method on the object
-            jstring jstr = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, containerGetDataStart, handle, (int)flags), _DW_REFERENCE_NONE);
-            if(jstr)
-                retval = (char *)env->GetStringUTFChars(jstr, nullptr);
+            jstring jstr = (jstring)_dw_jni_check_result(env, env->CallObjectMethod(_dw_obj, containerGetTitleNext, handle, (jint)flags), _DW_REFERENCE_NONE);
+            char *str;
+
+            if(jstr && (str = (char *)env->GetStringUTFChars(jstr, nullptr)))
+                retval = strdup(str);
         }
     }
-    return nullptr;
+    return retval;
 }
 
 /*