Android Question Problem With xCustomListView -View Initialization error

Johnson Samuel

Member
Licensed User
Longtime User
I have created a customListView with two labels and an editText View. The List is populated from a remote mysql database. I am also able to edit data in the edit text view. Now I am trying to save the data in the mysql database. two things take place:
1. If I scroll the list and then save the data it saves the data perfectly.
2. If I do not scroll and try to save the data; there is an error " java.lang.RuntimeException: Object should first be initialized (View)." why this behaviour? can any one explain . The code I am using is as below: errror is showing in line 29 below. The xCustomListView in from the designer.
B4X:
Sub CLV_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    clvchange=True
    Dim ExtraSize As Int = 25 'List size
    For i = Max(0, FirstIndex - ExtraSize) To Min(LastIndex + ExtraSize, CLV.Size - 1)
        Dim pnl As B4XView = CLV.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            If pnl.NumberOfViews = 0 Then 'Add each item/layout to the list/main layout
                Dim ID As studentData = CLV.GetValue(i)
                pnl.LoadLayout("frmschoinfo")
                LblRno.Text = ID.rno
                LblStudent.Text = ID.sname
                edtMark.Text = ID.mark
               
            End If
        Else 'Not visible
            If pnl.NumberOfViews > 0 Then
                pnl.RemoveAllViews 'Remove none visable item/layouts from the list/main layout
            End If
        End If
    Next
End Sub

Sub btnSave_Click
    'save data to database
       
    For j=0 To CLV.size -1
        dmap.Initialize
       
        dmap.Put("roll",CLV.GetPanel(j).GetView(0).text)
        dmap.Put("mark",CLV.GetPanel(j).GetView(2).text)
        data.Add(dmap)
   
    Next
    Wait For(AddMark ) Complete( ResStatus As String )
    If ResStatus.trim<>"" Then
        ToastMessageShow(ResStatus,True)
    Else
        ToastMessageShow("No Changes",True)
    End If
   
End Sub
 

Mahares

Expert
Licensed User
Longtime User
I will go thru the link and try.
I am going to take a stab at this thread because I had some trouble with lazy loading in the past and may still do. Erel talks about separating the UI from the program to get something like this to work. Here is how I refer it: Extract the data you need from the custom type items as opposed to getting it from the views which are not initialized. So here is my solution, and I would like anyone to chime in to correct if they think it is flawed: the btnSave Sub should be:
B4X:
Sub btnSave_Click
    'save data to database    
    For j=0 To clv2.size -1
        dmap.Initialize
        Dim ID As studentData = clv2.GetValue(j)
        dmap.Put("roll",ID.rno)       'or whatever  item you need from custom type
        dmap.Put("mark",ID.mark)  'or whatever  item you need from custom type
        data.Add(dmap)  
    Next
    Wait For(AddMark ) Complete( ResStatus As String )
    If ResStatus.trim<>"" Then
        ToastMessageShow(ResStatus,True)
    Else
        ToastMessageShow("No Changes",True)
    End If  
End Sub
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
This is unusual behavior of using lazy loading for inline edit data, as i know lazy loading good for focus on viewing data on specific index that we can see (visible range) in the CLV, but its okay... every body has his own way how they code.

Since only above code snipset for base information, first thing in my mind is - the error came because in btnSave_Click you populate ALL the item from first index till the end. which some of view on unvisible range.

My suggestion (not sure if will work) is save current visible index (FirstIndex dan LastIndex) to global variable (pay attention that you also set ExtraSize), and then when you want to save data, only lookup for those range variable only, imposible to get data from unvisible area.
 
Last edited:
Upvote 0

Johnson Samuel

Member
Licensed User
Longtime User
Your Reply is Correct. I had increased the ExtraSize value greater than the CLV size and it solved the problem. Thanks
 
Upvote 0
Top