changeset 2561:f28d7d0ca5ed

Android: Attempt at implementing updating the container after insertion. I am trying to use notifyDataSetChanged() in our DWContainerAdapter class. This is called via _dw_container_refresh() and containerRefresh() however it does not seem to be working yet.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 18 May 2021 22:48:33 +0000
parents 3da35cd91ca7
children 11411f103d27
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 96 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Tue May 18 18:41:35 2021 +0000
+++ b/android/DWindows.kt	Tue May 18 22:48:33 2021 +0000
@@ -642,35 +642,54 @@
     }
 
     override fun getView(position: Int, view: View?, parent: ViewGroup): View {
-        val rowView = ConstraintLayout(context)
-        val set = ConstraintSet()
-
-        // Every container at least has an icon and text
-        val imageview = ImageView(context)
-        val textview = TextView(context)
-
-        if(model.numberOfColumns() > 1 && model.numberOfRows() > position) {
-            val first = model.getRowAndColumn(position, 0)
-            val second = model.getRowAndColumn(position, 1)
-
-            if(first is Drawable) {
-                imageview.setImageDrawable(first)
+        var rowView: ConstraintLayout? = view as ConstraintLayout
+
+        // If the view passed in is null we need to create the layout
+        if(rowView == null) {
+            rowView = ConstraintLayout(context)
+            val set = ConstraintSet()
+            // Every container at least has an icon and text
+            val imageview = ImageView(context)
+            val textview = TextView(context)
+
+            if (model.numberOfColumns() > 1 && model.numberOfRows() > position) {
+                val first = model.getRowAndColumn(position, 0)
+                val second = model.getRowAndColumn(position, 1)
+
+                if (first is Drawable) {
+                    imageview.setImageDrawable(first)
+                }
+                if (second is String) {
+                    textview.text = second
+                }
             }
-            if(second is String) {
-                textview.text = second
+
+            // Add the two main components to the layout
+            imageview.id = View.generateViewId()
+            textview.id = View.generateViewId()
+            rowView.addView(imageview)
+            rowView.addView(textview)
+            set.clone(rowView)
+            set.connect(imageview.id, ConstraintSet.LEFT, textview.id, ConstraintSet.RIGHT)
+            set.applyTo(rowView)
+
+            // TODO: Add code to optionally add other columns
+        } else {
+            // Otherwise we just need to update the existing layout
+            if (model.numberOfColumns() > 1 && model.numberOfRows() > position) {
+                val first = model.getRowAndColumn(position, 0)
+                val second = model.getRowAndColumn(position, 1)
+                val imageview = rowView.getChildAt(0)
+                val textview = rowView.getChildAt(1)
+
+                if (first is Drawable && imageview is ImageView) {
+                    imageview.setImageDrawable(first)
+                }
+                if (second is String && textview is TextView) {
+                    textview.text = second
+                }
             }
         }
-
-        // Add the two main components to the layout
-        imageview.id = View.generateViewId()
-        textview.id = View.generateViewId()
-        rowView.addView(imageview)
-        rowView.addView(textview)
-        set.clone(rowView)
-        set.connect(imageview.id, ConstraintSet.LEFT, textview.id, ConstraintSet.RIGHT)
-        set.applyTo(rowView)
-
-        // TODO: Add code to optionally add other columns
         return rowView
     }
 }
@@ -2224,6 +2243,15 @@
         }
     }
 
+    fun containerRefresh(cont: ListView)
+    {
+        waitOnUiThread {
+            val adapter: DWContainerAdapter = cont.adapter as DWContainerAdapter
+
+            adapter.notifyDataSetChanged()
+        }
+    }
+
     fun containerGetColumnType(cont: ListView, column: Int): Int
     {
         var type: Int = 0
--- a/android/dw.cpp	Tue May 18 18:41:35 2021 +0000
+++ b/android/dw.cpp	Tue May 18 22:48:33 2021 +0000
@@ -3124,15 +3124,8 @@
     dw_container_change_item(handle, column, row + rowstart, data);
 }
 
-/*
- * Changes an existing item in specified row and column to the given data.
- * Parameters:
- *          handle: Handle to the container window (widget).
- *          column: Zero based column of data being set.
- *          row: Zero based row of data being set.
- *          data: Pointer to the data to be added.
- */
-void API dw_container_change_item(HWND handle, int column, int row, void *data)
+/* Internal version that doesn't update the list immediately */
+void API _dw_container_change_item(HWND handle, int column, int row, void *data)
 {
     JNIEnv *env;
 
@@ -3189,6 +3182,38 @@
     }
 }
 
+/* Notify that the data changed, causing the container to refresh */
+void _dw_container_refresh(HWND handle)
+{
+    JNIEnv *env;
+
+    if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // 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 containerRefresh = env->GetMethodID(clazz, "containerRefresh",
+                                                      "(Landroid/widget/ListView;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, containerRefresh, handle);
+        _dw_jni_check_exception(env);
+    }
+}
+
+/*
+ * Changes an existing item in specified row and column to the given data.
+ * Parameters:
+ *          handle: Handle to the container window (widget).
+ *          column: Zero based column of data being set.
+ *          row: Zero based row of data being set.
+ *          data: Pointer to the data to be added.
+ */
+void API dw_container_change_item(HWND handle, int column, int row, void *data)
+{
+    _dw_container_change_item(handle, column, row, data);
+    _dw_container_refresh(handle);
+}
+
 /*
  * Changes an existing item in specified row and column to the given data.
  * Parameters:
@@ -3199,7 +3224,8 @@
  */
 void API dw_filesystem_change_item(HWND handle, int column, int row, void *data)
 {
-    dw_container_change_item(handle, column + 2, row, data);
+    _dw_container_change_item(handle, column + 2, row, data);
+    _dw_container_refresh(handle);
 }
 
 /*
@@ -3213,8 +3239,9 @@
  */
 void API dw_filesystem_change_file(HWND handle, int row, const char *filename, HICN icon)
 {
-    dw_container_change_item(handle, 0, row, (void *)&icon);
-    dw_container_change_item(handle, 1, row, (void *)&filename);
+    _dw_container_change_item(handle, 0, row, (void *)&icon);
+    _dw_container_change_item(handle, 1, row, (void *)&filename);
+    _dw_container_refresh(handle);
 }
 
 /*
@@ -3414,6 +3441,7 @@
     if(pointer && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
         env->DeleteWeakGlobalRef((jobject)pointer);
     dw_window_set_data(handle, "_dw_rowstart", NULL);
+    _dw_container_refresh(handle);
 }
 
 /*
@@ -3436,6 +3464,7 @@
         // Call the method on the object
         env->CallVoidMethod(_dw_obj, containerClear, handle);
         _dw_jni_check_exception(env);
+        _dw_container_refresh(handle);
     }
 }
 
@@ -3459,6 +3488,7 @@
         // Call the method on the object
         env->CallVoidMethod(_dw_obj, containerDelete, handle, rowcount);
         _dw_jni_check_exception(env);
+        _dw_container_refresh(handle);
     }
 }