Clearing ScrollView

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
I am using a ScrollView similarly to the customscrollview.zip example:

http://www.b4x.com/forum/basic4andr...417-another-scrollview-example.html#post47107

I need to clear, then re-populate the ScrollView each time the page is opened.

Is there a way to clear or remove all the views from a ScrollView panel? Is looping over the index 0f views the best way?

Could I re-initialize the ScrollView to clear it or will that leave orphans?

A third way would be to remove the ScrollView entirely from its parent, then create a new one?

What method is best?

Thanks,
Barry.
 

klaus

Expert
Licensed User
Longtime User
Following routine is the easiets way.

Sub
ClearAll
Dim i As Int
For i = scvMain.Panel.NumberOfViews - 1 To 0 Step -1
scvMain.Panel.RemoveViewAt(i)
Next
scvMain.Panel.Height = 0
SelectedRow = -1
End Sub

If you have a variable for the selected row, SelectedRow, you must set to -1.

Best regards.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
ScrollView Clearing

Hi!

I'm having this kind of problem.

I have a scrollview that is populated with SQL from a table.
Following some forum (Klaus) examples, this is my sub:

B4X:
sub SQLReadTable
' Reads the SQL data base
   Dim i As Int
   Dim Cursor1 As Cursor

   Cursor1 = SQL1.ExecQuery("SELECT IdArt,DesArt,Um,QtaOrd,ImpUni from ORDINI")

   Dim row(NumberOfColumns) As String
   row(0)="Codice"
   row(1)="Descrizione"
   row(2)="Um"
   row(3)="Qtà"
   row(4)="Imp. Unit."
      
   SetHeader(row)
   NumberOfRows=0
   For i = 0 To Cursor1.RowCount - 1
      Dim row(NumberOfColumns) As String
      Cursor1.Position = i
      row(0)=Cursor1.GetString("IdArt")
      row(1)=Cursor1.GetString("DesArt")
      row(2)=Cursor1.GetString("Um")
      row(3)=Cursor1.GetString("QtaOrd")
      row(4)=Cursor1.GetString("ImpUni")
      AddRow(Row)
   Next
   Cursor1.Close
End Sub

It works. But, if i add a new record in the db (using another module) when I raise Activity Resume I recall
B4X:
Sub Activity_Resume
   SQLReadTable
   NumberOfVisibleRows=scvList.Height/RowHeight
End Sub

the new record is visible on the scroll view but not clickable!
Maybe I need to redraw the scrollview again, tried with invalidate before SQL ReadTable but no success!

If I Try

B4X:
Sub Activity_Resume
ClearAll
SQLReadTable
   NumberOfVisibleRows=scvList.Height/RowHeight
End Sub

Nothing seems to happen.
How can I achieve that?
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately you don't give enough information.
You should, at least, also post the code of Activity_Create and Activity_Resume to know exactly what you are doing and where.
I suspect that you call two times SQLReadTable, and then you have all views two times but the top one is no more clickable.

Best regards.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
here they are....

my subs:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Ordine")
   
   '********************************* creazione numero e data ordine
   Dim now As Long
   now = DateTime.Now
   'normalizziamo la data
   If DateTime.GetDayOfMonth(now) < 10 Then
      TxtDataOrd.Text = "0"&DateTime.GetDayOfMonth(now)
   Else
      TxtDataOrd.Text = "0"&DateTime.GetDayOfMonth(now)
   End If
   If DateTime.GetMonth(now) < 10 Then
      TxtDataOrd.Text = TxtDataOrd.Text&"/0"&DateTime.GetMonth(now)
   Else
      TxtDataOrd.Text = TxtDataOrd.Text&"/"&DateTime.GetMonth(now)
   End If
   TxtDataOrd.Text = TxtDataOrd.Text&"/"&DateTime.GetYear(now)
   
   TxtOraOrd.Text = ""&DateTime.Time(now)
   'normalizziamo il numero ordine
   TxtIdOrd.Text = ""&DateTime.GetYear(now)
   If DateTime.GetMonth(now) < 10 Then
      TxtIdOrd.Text = TxtIdOrd.Text&"0"&DateTime.GetMonth(now)
   Else
      TxtIdOrd.Text = TxtIdOrd.Text&""&DateTime.GetMonth(now)
   End If
   If DateTime.GetMonth(now) < 10 Then
      TxtIdOrd.Text = TxtIdOrd.Text&"0"&DateTime.GetDayOfMonth(now)
   Else
      TxtIdOrd.Text = TxtIdOrd.Text&""&DateTime.GetDayOfMonth(now)
   End If
   If DateTime.GetHour(now) < 10 Then
      TxtIdOrd.Text = TxtIdOrd.Text&"0"&DateTime.GetHour(now)
   Else
      TxtIdOrd.Text = TxtIdOrd.Text&""&DateTime.GetHour(now)
   End If
   If DateTime.GetMinute(now) < 10 Then
      TxtIdOrd.Text = TxtIdOrd.Text&"0"&DateTime.GetMinute(now)
   Else
      TxtIdOrd.Text = TxtIdOrd.Text&""&DateTime.GetMinute(now)
   End If
   If DateTime.GetSecond(now) < 10 Then
      TxtIdOrd.Text = TxtIdOrd.Text&"0"&DateTime.GetSecond(now)
   Else
      TxtIdOrd.Text = TxtIdOrd.Text&""&DateTime.GetSecond(now)
   End If
   '********************************* fine creazione numero e data ordine   
   
   scvList.Panel.Color=Colors.Black
   Table = scvList.Panel
   Table.Color = LineColor

'   ColumnWidth(0) = ColLineWidth               ' hides the first column
'   ColumnWidth(1) = scvList.Width / 2      ' sets the remaining column widths 1/2 screen width
'   ColumnWidth(2) = scvList.Width / 2
   ColumnWidth(0) = 0.2 * scvList.Width      ' sets the column widths 1/3 screen width
   ColumnWidth(1) = 0.4 * scvList.Width
   ColumnWidth(2) = 0.1 * scvList.Width
   ColumnWidth(3) = 0.1 * scvList.Width
   ColumnWidth(4) = 0.2 * scvList.Width
   
   For i=0 To NumberOfColumns-1
      ' sets the label width to the column width minus column line width
      ColumnWidth_1(i) = ColumnWidth(i)-ColLineWidth   
   Next
   SelectedItems.Initialize
   ' leggiamo i record in questione dal db
   SQLReadTable
   NumberOfVisibleRows=scvList.Height/RowHeight

End Sub

and

B4X:
Sub Activity_Resume
   ClearAll
   SQLReadTable
   NumberOfVisibleRows=scvList.Height/RowHeight
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Your code confirms what I suspected, you are reading two times the Table.
Remove the last two lines in Activity_Create
B4X:
SQLReadTable
NumberOfVisibleRows=scvList.Height/RowHeight
and leave the two same lines in Activity_Resume.

Best regards.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Thank You Klaus!

works perfectly!!!!

only one "small" problem

if i delete all the items in the scroll view, when I delete the last one (the scroll view is empty) a black line appears (like the first line!)

I've changed the background of the scrollview to white from black, do i have to set another property???
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I left at least on empty line with its background color.
If you want to remove it replace in Sub List_Delete(Row As Int) at the end:
B4X:
   If NumberOfRows > 0 Then                ' updates the ScrollView Panel height
        pnlTable.Height = NumberOfRows * RowHeight
    Else
        pnlTable.Height = RowHeight
    End If
by
B4X:
    pnlTable.Height = NumberOfRows * RowHeight
Best regards.
 
Upvote 0
Top