How to load ScrollView from database with repeating panels

jkurant

Member
Licensed User
Longtime User
I want to display a list of reminders in a ScrollView. Each reminder will be displayed on a panel that contains three labels, in various fonts and colors. I have successfully added two panels with the same data onto the ScrollView, but I don't know how to access the labels on the panel so I can set the text I want to display.

Here is my code

B4X:
   Dim rs As Cursor
   Dim SQL As String
   SQL = "SELECT ID, [Description], [When], [Trigger], PlaceID, Mon, Tue, Wed, Thu, Fri, Sat, Sun, StartTimeWindowHours, StartTimeWindowMinutes, StopTimeWindowHours, StopTimeWindowMinutes FROM Reminder ORDER BY [Name]"
   rs = SQLiteUtils.DbExecQuery(SQL)
   Dim i As Int
   For i = 0 To rs.RowCount-1
      rs.Position = i
      Dim p As Panel
      p.Initialize("PanelReminder")
      p.LoadLayout("ReminderListPanel")
      p.Tag = "1"
      p.lblReminderName.Text = ' This doesn't work
      svReminders.Panel.AddView(p, 0, 0, InsideTabWidth, 95dip)
      MyPlaces.Add(p)
   Next
 

jkurant

Member
Licensed User
Longtime User
I want to know when the user has clicked on a particular panel and I find I can capture the Panel_Click event and get the correct ID out of the Tag property of the Panel in Sender.

The problem is I cannot set the text of the labels on the panels to the properties of the reminders I am displaying.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several ways to do it.
One way it to declare a type such as:
B4X:
Type PanelLabels (Label1 As Label, Label2 As Label, Label3 As Label)
Later when you add the labels to the panel, you should create a new variable of this type, assign the labels and set it as the panel's Tag property.
This will allow you to easily get a reference to the three labels.
 
Upvote 0

salvatore75

Member
Licensed User
Longtime User
There are several ways to do it.
One way it to declare a type such as:
B4X:
Type PanelLabels (Label1 As Label, Label2 As Label, Label3 As Label)
Later when you add the labels to the panel, you should create a new variable of this type, assign the labels and set it as the panel's Tag property.
This will allow you to easily get a reference to the three labels.

Erel can you post an example for type panelLabels?
I want to load a Scrollview from a database with a query iteratively the labels of the panel.

Thank you
 
Upvote 0

jkurant

Member
Licensed User
Longtime User
I just want to set the values, not remember them

I want to write to the labels, not read them.

I don't need to be able to find out what the values are of the labels when the user selects a panel from the ScrollView. I have set the panel's Tag property to the ID of the row from the database, so I know I can get that record when the user clicks on the panel in the scrollview. What I want is to be able to set the Text property of the labels on the panel from the database.

I am getting the layout for each panel from a layout I made in the designer. It has the three labels on it. I load that onto the panel with LoadLayout. But then I don't have a way to set the text on the labels.

Based on your saying, "When you add the labels to the panel", it is starting to sound like I have to create and add the labels programmatically, rather than using the layout at all. I hope this is not true.

So, I want to load the panel from a layout and then set the Text properties of the labels from the database.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is a working example. The layout file includes three labels named Label1, Label2 and Label3.

B4X:
Sub Process_Globals
   Type pnlLabels (lbl1 As Label, lbl2 As Label, lbl3 As Label)
End Sub
Sub Globals
   Dim Label1 As Label
   Dim Label2 As Label
   Dim Label3 As Label
   Dim sv As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   sv.Initialize(2000dip)
   Activity.AddView(sv, 0, 0, 100%x, 100%y)
   For i = 0 To 100
      Dim p As Panel
      p.Initialize("pnl")
      p.LoadLayout("1")
      'Label1, 2, 3 will now reference the labels that were currently added
      Dim pl As pnlLabels
      pl.lbl1 = Label1
      pl.lbl2 = Label2
      pl.lbl3 = Label3
      p.Tag = pl
      sv.Panel.AddView(p, 0, 200dip * i, sv.Width, 200dip)
   Next
End Sub

Sub pnl_Click
   Dim pl As pnlLabels
   Dim p As Panel
   p = Sender
   pl = p.Tag
   pl.lbl1.Text = "123"
   pl.lbl2.Text = "456"
   pl.lbl3.Text = "789"
End Sub
 
Upvote 0
Top