Jump to content

Listview... slow to load item


Recommended Posts

Hello,

how, can I load 1000 records in a listview more speedly as soon as possible?

I post my code...

Func _Clienti()
    Local $hQuery, $aRow, $nMsg
    Local $hReBar_Clienti, $hToolBarImageListNorm
    Local $j = 0, $iItemIndex, $aSize, $aStrings[10]
    Local $id1 = 1000, $id2 = 1001, $id3 = 1002, $id4 = 1003, $id5 = 1004, $id6 = 1005, $id7 = 1006, $id8 = 1007, $id9 = 1008; ID's for each ToolBar button
    $hGUI_Clienti = GUICreate("Clienti", 980, 270)
    GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
    $hReBar_Clienti = _GUICtrlRebar_Create($hGUI_Clienti, BitOR($CCS_TOP, $RBS_VARHEIGHT, $RBS_AUTOSIZE, $RBS_BANDBORDERS))
    $hToolbar_Clienti = _GUICtrlToolbar_Create($hGUI_Clienti, BitOR($TBSTYLE_FLAT, $BTNS_AUTOSIZE, $BTNS_BUTTON, $BTNS_SHOWTEXT)); BitOR($BTNS_AUTOSIZE, $BTNS_BUTTON, $BTNS_SHOWTEXT)
    $aSize = _GUICtrlToolbar_GetMaxSize($hToolbar_Clienti)
; Add standard system bitmaps
    Switch _GUICtrlToolbar_GetBitmapFlags($hToolbar_Clienti)
        Case 0
            _GUICtrlToolbar_AddBitmap($hToolbar_Clienti, 1, -1, $IDB_STD_SMALL_COLOR)
        Case 2
            _GUICtrlToolbar_AddBitmap($hToolbar_Clienti, 1, -1, $IDB_STD_LARGE_COLOR)
    EndSwitch
; Create ToolTip
    $hToolTip_Clienti = _GUIToolTip_Create($hToolbar_Clienti, $TTS_ALWAYSTIP)
    _GUICtrlToolbar_SetToolTips($hToolbar_Clienti, $hToolTip_Clienti)
; Add Icons
    $hToolBarImageListNorm = _GUIImageList_Create(32, 32)
    _GUIImageList_AddBitmap($hToolBarImageListNorm, @ScriptDir & "\Icone\Nuovo.bmp")
    _GUIImageList_AddBitmap($hToolBarImageListNorm, @ScriptDir & "\Icone\Modifica.bmp")
    _GUIImageList_AddBitmap($hToolBarImageListNorm, @ScriptDir & "\Icone\Stampa.bmp")
    _GUIImageList_AddBitmap($hToolBarImageListNorm, @ScriptDir & "\Icone\Elimina.bmp")
    _GUIImageList_AddBitmap($hToolBarImageListNorm, @ScriptDir & "\Icone\Uscita.bmp")
    _GUICtrlToolbar_SetImageList($hToolbar_Clienti, $hToolBarImageListNorm)
; Add buttons
    _GUICtrlToolbar_AddButton($hToolbar_Clienti, $id1, 0, $aStrings[0]); Index 0
    _GUICtrlToolbar_AddButton($hToolbar_Clienti, $id2, 1, $aStrings[1]); Index 1
    _GUICtrlToolbar_AddButton($hToolbar_Clienti, $id3, 2, $aStrings[2]); Index 2
    _GUICtrlToolbar_AddButton($hToolbar_Clienti, $id4, 3, $aStrings[3]); Index 3
    _GUICtrlToolbar_AddButton($hToolbar_Clienti, $id5, 4, $aStrings[4]); Index 4
    _GUICtrlRebar_AddToolBarBand($hReBar_Clienti, $hToolbar_Clienti)
    
    
    $hListView_Clienti = GUICtrlCreateListView("", 10, 55, 960, 194)
    
    _GUICtrlListView_InsertColumn($hListView_Clienti, 0, "Codice", 50)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 1, "Cognome", 150)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 2, "Nome", 150)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 3, "Città", 80)
    
    _GUICtrlListView_InsertColumn($hListView_Clienti, 4, "Cap", 50)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 5, "Indirizzo", 150)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 6, "Telefono", 80)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 7, "Sesso", 50)
    
    _GUICtrlListView_InsertColumn($hListView_Clienti, 8, "Data Nascita", 80)
    
    _GUICtrlListView_InsertColumn($hListView_Clienti, 9, "Sospesi", 30)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 10, "Listino", 30)
    
    _GUICtrlListView_SetExtendedListViewStyle($hListView_Clienti, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_FLATSB, $LVS_EX_REGIONAL, $LVS_EX_DOUBLEBUFFER))
    _GUICtrlListView_SetBkColor($hListView_Clienti, $CLR_MONEYGREEN)
    _GUICtrlListView_SetTextColor($hListView_Clienti, $CLR_BLACK)
    _GUICtrlListView_SetTextBkColor($hListView_Clienti, $CLR_MONEYGREEN)
    _SQLite_Query(-1, "SELECT * FROM Clienti ORDER BY cognome, nome;", $hQuery); rowId  the query
    GUISetState(@SW_SHOW)
;~ _GUICtrlListView_BeginUpdate($hListView_Clienti)
    While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK
        _GUICtrlListView_AddItem($hListView_Clienti, Number($aRow[0]))
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[1], 1)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[2], 2)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[3], 3)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[4], 4)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[5], 5)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[6], 6)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[7], 7)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, _data($aRow[8]), 8)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[9], 9)
        _GUICtrlListView_AddSubItem($hListView_Clienti, $j, $aRow[10], 10)
        
        $j += 1
    WEnd
;~ _GUICtrlListView_EndUpdate($hListView_Clienti)
    ControlFocus($hGUI_Clienti, "", $hListView_Clienti)
;~  _GUICtrlListView_SetItemFocused($hListView_Clienti, 0)
;~  _GUICtrlListView_SetItemSelected($hListView_Clienti, 0)
;$iItemIndex = Number(_GUICtrlListView_GetSelectedIndices($hListView_Clienti))
    
    
EndFunc  ;==>_Clienti

The func _Clienti() use the database SQLLITE.

Thank you in advance.

Roby

Link to comment
Share on other sites

I would create a loop that iterated through queries against your database to grab the next item to add to the listview, then add each item with _GUICtrlListView_AddItem()

If you wanted to optimize viewing time, however, you'd be best off by creating a view controller and only grabbing 10 or so at a time, as opposed to grabbing and setting them all at once. Create 1000 dummy records, and only update the view based on where the user is actually looking. Leave existing items in place, however, and that way the user gets a seamless experience without actually having to load all 1000 (or however many) records at once.

Link to comment
Share on other sites

If you complete your database queries and assemble a 2D array of Item/SubItem data, separately from building the ListView, then it will completely load in about 1.3 seconds (on my computer):

#include <GuiConstants.au3>
#include <GuiListView.au3>

Global $hGUI_Clienti, $ctrlListView_Clienti, $hListView_Clienti
Global $avData[100][11]

; Simulates loading data from SQLite DB into array
For $n = 0 To 99
    $avData[$n][0] = "Item: " & $n
    For $x = 1 To 10
        $avData[$n][$x] = "SubItem: " & $n & ":" & $x
    Next
Next

; Create GUI and load ListView
_Clienti()

Do
    Sleep(20)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _Clienti()
    $hGUI_Clienti = GUICreate("Clienti", 980, 270)

    $ctrlListView_Clienti = GUICtrlCreateListView("", 10, 55, 960, 194)
    $hListView_Clienti = ControlGetHandle($hGUI_Clienti, "", $ctrlListView_Clienti)
    
    _GUICtrlListView_InsertColumn($hListView_Clienti, 0, "Codice", 50)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 1, "Cognome", 150)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 2, "Nome", 150)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 3, "Città", 80)

    _GUICtrlListView_InsertColumn($hListView_Clienti, 4, "Cap", 50)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 5, "Indirizzo", 150)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 6, "Telefono", 80)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 7, "Sesso", 50)

    _GUICtrlListView_InsertColumn($hListView_Clienti, 8, "Data Nascita", 80)

    _GUICtrlListView_InsertColumn($hListView_Clienti, 9, "Sospesi", 30)
    _GUICtrlListView_InsertColumn($hListView_Clienti, 10, "Listino", 30)
    _GUICtrlListView_SetExtendedListViewStyle($hListView_Clienti, BitOR($LVS_EX_GRIDLINES, _
        $LVS_EX_FULLROWSELECT, $LVS_EX_FLATSB, $LVS_EX_REGIONAL, $LVS_EX_DOUBLEBUFFER))
    GUISetState(@SW_SHOW)
    Local $iTimer = TimerInit()
    For $n = 0 To 99
        _GUICtrlListView_AddItem($hListView_Clienti, $avData[$n][0])
        For $x = 1 To 10
            _GUICtrlListView_AddSubItem($hListView_Clienti, $n, $avData[$n][$x], $x)
        Next
    Next
    WinSetTitle($hGUI_Clienti, "", "1000 SubItems added in " & Round(TimerDiff($iTimer) / 1000, 3) & " seconds")
EndFunc ;==>_Clienti

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

@PsaltyDS

If you have 2D array, then instead of _GUICtrlListView_AddItem & _GUICtrlListView_AddSubItem loops, it would be faster to use _GUICtrlListView_AddArray() which exists for this very purpose - to get rid of the repetitive overhead (func calls, create struct, fill struct, etc) of adding subitems one by one.

Also, GUICtrlCreateListViewItem is obviously much faster than UDFs, why not use it.

There are several topics regarding speed of filling a listview, with various examples and techniques, if only OP bothered to use forum search. But bumping own topic in hopes someone else will do all the work is soo much better, right? Wrong.

"be smart, drink your wine"

Link to comment
Share on other sites

@PsaltyDS

If you have 2D array, then instead of _GUICtrlListView_AddItem & _GUICtrlListView_AddSubItem loops, it would be faster to use _GUICtrlListView_AddArray() which exists for this very purpose - to get rid of the repetitive overhead (func calls, create struct, fill struct, etc) of adding subitems one by one.

Also, GUICtrlCreateListViewItem is obviously much faster than UDFs, why not use it.

There are several topics regarding speed of filling a listview, with various examples and techniques, if only OP bothered to use forum search. But bumping own topic in hopes someone else will do all the work is soo much better, right? Wrong.

Sho 'nuf...

Using the native function took 0.9 seconds, better but some time is lost assembling the string for each item:

Local $iTimer = TimerInit(), $sItem
    For $n = 0 To 99
        $sItem = $avData[$n][0]
        For $x = 1 To 10
            $sItem &= "|" & $avData[$n][$x]
        Next
        GUICtrlCreateListViewItem($sItem, $ctrlListView_Clienti)
    Next
    WinSetTitle($hGUI_Clienti, "", "1000 SubItems added in " & Round(TimerDiff($iTimer) / 1000, 3) & " seconds")

...and this one only took 0.4 seconds:

Local $iTimer = TimerInit()
    _GUICtrlListView_AddArray($hListView_Clienti, $avData)
    WinSetTitle($hGUI_Clienti, "", "1000 SubItems added in " & Round(TimerDiff($iTimer) / 1000, 3) & " seconds")

"Learn something new every day." -- I can go back to bed now...

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...