iOS Question B4XTable - Photos Album has problem in B4i

roddy

Member
Licensed User
Longtime User
I reference this thread. ( B4XTable - Photos Album)

My APP work fine in B4A, But in B4i has some trouble.

Loading data for the first time is Okay.
The second reload data B4XTable becomes blank.
The app crashed after loading the data for the third time

Logs...
B4X:
Application_Start
Class (b4i_bcpath) instance released.
101.9925
已下載 3 筆資料
Class (b4i_httpjob) instance released.
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/01.jpg
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/02.jpg
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/03.jpg
Class (b4i_httpjob) instance released.
已下載 3 筆資料
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/01.jpg
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/02.jpg
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/03.jpg
Class (b4i_httpjob) instance released.
closing old db.
已下載 3 筆資料
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/01.jpg
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/02.jpg
Class (b4i_httpjob) instance released.
Current link: http://kaduo.xyz/andpic/03.jpg
Class (b4i_httpjob) instance released.
Error occurred on line: 560 (B4XTable)
*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
Stack Trace: (
  CoreFoundation       __exceptionPreprocess + 242
  libobjc.A.dylib      objc_exception_throw + 48
  CoreFoundation       _CFThrowFormattedException + 194
  CoreFoundation       -[__NSArrayM objectAtIndex:] + 142
  result               -[B4IList Get:] + 92
  result               -[b4i_b4xtable _getlabelfromcolumn:::] + 704
  result               -[b4i_b4xtable _addcolumn:::] + 1595
  result               -[b4i_pagesev1 _createtable::] + 1400
  result               -[ResumableSub_pagesev1_FindPictures resume::] + 1682
  CoreFoundation       __invoking___ + 140
CoreFoundation       -[NSInvocation invoke] + 303
result               +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1669
result               -[B4IShell runMethod:] + 401
result               -[B4IShell raiseEventImpl:method:args::] + 2361
result               -[B4IShellBI raiseEvent:event:params:] + 1357
result               -[B4IDelegatableResumableSub resume::] + 378
result               -[B4I checkAndRunWaitForEvent:event:params:] + 500
result               -[B4IShellBI raiseEvent:event:params:] + 1204
result               __37-[B4ICommon ReturnFromResumableSub::]_block_invoke + 300
libdispatch.dylib    _dispatch_call_block_and_release + 12
libdispatch.dylib    _dispatch_client_callout + 8
libdispatch.dylib    _dispatch_main_queue_callback_4CF + 1045
CoreFoundation       __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
CoreFoundation       __CFRunLoopRun + 2685
CoreFoundation       CFRunLoopRunSpecific + 567
GraphicsServices     GSEventRunModal + 139
UIKitCore            -[UIApplication _run] + 912
UIKitCore            UIApplicationMain + 101
result               main + 112
 

roddy

Member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    Private CLV1 As CustomListView
    Private LBsub As B4XView
    Private B4XTable1 As B4XTable
    Private Load1 As B4XLoadingIndicator

    Private URL_str As String
    Private CityName As B4XView
    Public datas As List
    Public Page71sub As PageSev1sub
    Private NumberOfColumns As Int = 4   
    Private TableHight As Int = 0

End Sub

Public Sub Initialize As Object
    Return Me
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("Page71")
    
    datas.Initialize
    Load1.Show

    Wait For (getGA)  Complete (CountyList As List)
    If CountyList.Size>0 Then
        For i=0 To CountyList.Size-1
            Dim m As Map
            m = CountyList.Get(i)
            CLV1.Add(CreateListItem1(m.Get("GA"), CLV1.AsView.Width, 42dip),m.Get("GA"))
        Next
    End If
    Load1.Hide
End Sub

Sub CreateListItem1(City As String, Width As Int, Height As Int ) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0 , 0, 0, Width, Height)
    p.LoadLayout("Menu1CLV1item")
    CityName.Text = City
    Return p
End Sub

Sub getGA() As ResumableSub
    URL_str = Main.ServerIP & "Hairselect.aspx"
    Dim Para() As String
    Dim P1 As String = "SELECT GA FROM APhairGA"
    Para = Array As String("sele",P1)
    Dim Job As HttpJob
    Job.Initialize(Null, Me)
    Job.Download2(URL_str,Para)
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Dim parser As JSONParser
        Dim response As String
        response = Job.GetString
        parser.Initialize(response)
        Dim rows As List
        rows = parser.NextArray
        Log(rows.Size)
    Else
        Log("error.." )
    End If
    Job.Release
    Return rows
End Sub

Sub CLV1_ItemClick (Index As Int, Value As Object)
    datas.Clear       
    B4XTable1.Clear       
    Main.FilesList.Clear   
    LBsub.Text = Value   
    FindPictures(Value)   
End Sub

Sub FindPictures(nGA As String)
    Load1.Show
    '取得檔名列表
    Wait For (getGAsub(nGA)) Complete (Completed1 As Boolean)
    If Completed1 Then
        'Download ALL Picture
        Wait For (DownloadPic(datas)) Complete (Completed2 As Boolean)
        If Completed2 Then
            CreateTable(Main.FilesList)
        End If
    End If
    Load1.Hide
End Sub

Sub getGAsub(vGA As String) As ResumableSub
    Dim ret As Boolean
    URL_str = Main.ServerIP & "Hairselect.aspx"
    Dim Para() As String
    Dim P1 As String = "SELECT 圖片 FROM APhairPic where GA='" & vGA & "'"
    Para = Array As String("sele",P1)
    Dim Job As HttpJob
    Job.Initialize(Null, Me)
    Job.Download2(URL_str,Para)
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Dim parser As JSONParser
        Dim response As String
        response = Job.GetString
        parser.Initialize(response)
        Dim rows As List
        rows = parser.NextArray
        For i=0 To rows.Size-1
            Dim m As Map
            m = rows.Get(i)
            datas.Add(m.Get("圖片"))
        Next
        ret = True
        Log(rows.Size)
    Else
        ret = False
        Log("error.." )
    End If
    Job.Release
    Return ret
End Sub

Sub DownloadPic(links As List) As ResumableSub
    Dim i As Int = 0
    For Each link As String In links
        Dim j As HttpJob
        j.Initialize("", Me) 'name is empty as it is no longer needed
        j.Download(link)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log("Current link: " & link)
            Dim out As OutputStream = File.OpenOutput(File.DirTemp, i & ".jpg", False)
            File.Copy2(j.GetInputStream, out)
            out.Close '<------ very important
            Main.FilesList.Add(i & ".jpg")
            i = i + 1
        End If
        j.Release
    Next
    Return True
End Sub

Sub CreateTable (Files As List)
    B4XTable1.AllowSmallRowHeightModifications = False
    B4XTable1.RowHeight = 100%x / NumberOfColumns * 1.2   
    B4XTable1.SearchVisible = False   
    B4XTable1.HeadersHeight = 0       
    If TableHight=0 Then           
        TableHight = B4XTable1.pnlHeader.Height
        B4XTable1.pnlHeader.Height = B4XTable1.pnlHeader.Height * 0.7
    End If
    
    For i = 0 To NumberOfColumns - 1
        B4XTable1.AddColumn(i, B4XTable1.COLUMN_TYPE_TEXT)
    Next
    'Force the creation of the cells.
    B4XTable1.MaximumRowsPerPage = 20
    B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
    For Each col As B4XTableColumn In B4XTable1.Columns
        col.Sortable = False
        col.Width = 100%x / NumberOfColumns
        
        For i = 0 To col.CellsLayouts.Size - 1
            Dim pnl As B4XView = col.CellsLayouts.Get(i)
            pnl.GetView(0).Visible = False 'hide the label
            If i > 0 Then 'i = 0 is the header
                Dim iv As ImageView
                iv.Initialize("")
                pnl.AddView(iv, 2dip, 2dip, col.Width - 4dip, B4XTable1.RowHeight - 4dip)
            End If
        Next
    Next
    LoadData(Files)
    B4XTable1.mBase.Visible = True
End Sub

Private Sub LoadData (Files As List)
    Dim Data As List
    Data.Initialize
    Dim row(NumberOfColumns) As Object
    Dim index As Int = -1
    For Each f As String In Files
        If f.EndsWith(".jpg") Or f.EndsWith(".png") Then
            index = (index + 1) Mod NumberOfColumns
            row(index) = f
            If index = NumberOfColumns - 1 Then
                Data.Add(row)
                Dim row(NumberOfColumns) As Object
            End If
        End If
    Next
    If index < NumberOfColumns - 1 Then
        'last incomplete row
        For i = index + 1 To NumberOfColumns - 1
            row(i) = ""
        Next
        Data.Add(row)
    End If
    B4XTable1.SetData(Data)
End Sub

Sub B4XTable1_DataUpdated
    Dim row As Map
    For i = 0 To B4XTable1.VisibleRowIds.Size - 1
        Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
        If RowId > 0 Then
            row = B4XTable1.GetRow(RowId)
        Else
            row = CreateMap()
        End If
        For Each column As B4XTableColumn In B4XTable1.Columns
            Dim pnl As B4XView = column.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
            Dim iv As B4XView = pnl.GetView(1) 'ImageView will be the 2nd child of the panel. The built-in label is the first.
            If row.GetDefault(column.Id, "") <> "" Then
                iv.SetBitmap(xui.LoadBitmapResize(File.DirTemp, row.Get(column.Id), iv.Width, iv.Height, False))
            Else
                iv.SetBitmap(Null)
            End If
        Next
    Next
    
End Sub
 
Upvote 0

roddy

Member
Licensed User
Longtime User
This is too much code for me to read like this. Sorry.

that's okay. It's solved.

I wanted to create a small example for you to test. Found that the column width is wrong during the process.

Original code (Line 149)
B4XTable1.RowHeight = 100%x / NumberOfColumns * 1.2

Why would 100%x become 0 ?
And the same code is normal in B4A ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…