Android Example Graphics ListView with 2 images, two labels and populated from a text file.

I wanted to provide this simple code for beginners like me.
The code is derived from the one written by the my teacher Klaus here.

This ListView is made with a ScrollView inside of which is placed a Panel. Inside the panel are added to images that make up the item.

In this case I used 2 images, one for the first item and one for everyone else. it is only a question graphics but you can use a single image.

For each item, there are two labels and are populated from a List, which in turn is populated from a text file.

I entered everything in a panel that then manage with only one property. Visible

If you use many types of text files, but only use a scrollView, remember to clean it before you populate it, otherwise the item sommerete continuously.

PS. Sorry for my english:)

B4X:
#Region  Project Attributes
    #ApplicationLabel: listviewex
    #VersionCode: 1
    #VersionName: 1.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: true
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: false
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim  panlist As Panel
    Dim scrolllista As ScrollView
    Dim labtitle As Label
    Dim item As Int
 

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
 
    Activity.LoadLayout("main")
 
    panlist.SetLayout(0%x, 0%y, 100%x, 100%y)'with the visible property, managed the panel
    labtitle.SetLayout(0%x, 5%y, 100%x, 10%y)
    scrolllista.Initialize(500)
    panlist.AddView(scrolllista,0%x,16%y,100%x,84%y)

    crealista

End Sub









Sub crealista
    'scrolllista.Panel.RemoveAllViews  ' Write this line if you have many text files and one scrolview. Clear Previous List
    Dim Bitmap1 As Bitmap
    Dim Panel0 As Panel
    Dim PanelTop, PanelHeight  As Int
    Dim lista As List
 
    lista=File.ReadList(File.DirAssets,"listlabel.txt")
 
 
    Bitmap1.Initialize(File.DirAssets,"banner1.png") ' First image of list
    PanelTop=1%y
    Panel0=scrolllista.Panel
    Panel0.Color=Colors.argb(0,0,0,0)  'sets the invisible panel
 
    For i=0 To lista.Size-1
 
        If i>0 AND i<3 Then Bitmap1.Initialize(File.DirAssets,"banner.png")  'Images beyond the first. Only if you use 2 images and 2 label
        Dim ImageView1 As ImageView
        ImageView1.Initialize("View")     
        PanelHeight=12%y
     
        Panel0.AddView(ImageView1,5%x,PanelTop,90%x,PanelHeight)

        ImageView1.Tag=i&"1"
        ImageView1.Bitmap=Bitmap1
        ImageView1.Gravity=Gravity.fill
     
        Dim Label1, Label2 As Label
        Label1.Initialize("")
        Label2.Initialize("")
        Panel0.AddView(Label1,5%x,PanelTop-2%y,90%x,PanelHeight)
        Panel0.AddView(Label2,5%x,PanelTop+2%y,90%x,PanelHeight)
     
        Label1.TextColor= Colors.black
        Label1.TextSize= 17 
        Label1.Gravity=Gravity.CENTER
        Label1.Color=Colors.argb(0,0,0,0)
        Label1.Text=lista.Get(i)

        Label2.TextColor= Colors.black
        Label2.TextSize= 15 
        Label2.Gravity=Gravity.CENTER
        Label2.Color=Colors.argb(0,0,0,0)
        Label2.Text=lista.Get(i+1)
     
        i=i+1 
     
        If i > lista.size-1 Then i = lista.size-1
     
     
        PanelTop=PanelTop+PanelHeight
    Next
    Panel0.Height=PanelTop
 
End Sub 

Sub View_Click
    Dim Send As View
    Dim row As Int
    Send=Sender
 
    row=Floor(Send.Tag/20)
        item=row
 
    labtitle.Text="Item "&item
     
End Sub



Sub Activity_Resume
 

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 

End Sub
 

Attachments

  • list viewex.zip
    109.6 KB · Views: 935
  • Screenshot_2014-05-19-22-05.png
    Screenshot_2014-05-19-22-05.png
    164.8 KB · Views: 913
Last edited:

ibra939

Active Member
Licensed User
Longtime User
Thanxs
 
Top