Android Tutorial ListView tutorial

electronik54

Member
Licensed User
Longtime User
You don't seem to be assigning a value to FindRow, you are Dimming Position as Int which defaults to 0, then assigning that to FindRow, which will mean the first row will always be returned.


You should also test that Cursor15.rowcount >= Findrow otherwise an error will occur, it probably will be if you are using the same list, but better safe than sorry.

i have taken care of empty cursor using If Cursor15.rowcount>0 Then i just need help with my above mentioned problem everything else works fine. i just need help with the code
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
OK, but that comes after the assignment of

B4X:
cursor15.rowcount = FindRow

But that was just an observation, My first paragraph had a suggestion that may provide help with your code.
 

klaus

Expert
Licensed User
Longtime User
You should put these lines out of the loop.
The layout definitions are the same for all entries.
B4X:
FA_s_lv.ScrollingBackgroundColor = Colors.White
FA_s_lv.TwoLinesAndBitmap.Label.TextSize = 15
FA_s_lv.TwoLinesAndBitmap.Label.TextColor = Colors.Black
FA_s_lv.TwoLinesAndBitmap.SecondLabel.TextSize = 13
FA_s_lv.TwoLinesAndBitmap.SecondLabel.TextColor = Colors.DarkGray
What are Position and Findrow supposed to do ?
You are doing nothing with them in the routine.

The code should work OK, what do you expect.
If you click on an item in the ListView what do you get ?

Best regards.
 

electronik54

Member
Licensed User
Longtime User
OK, but that comes after the assignment of

B4X:
cursor15.rowcount = FindRow

But that was just an observation, My first paragraph had a suggestion that may provide help with your code.

yup you pointed out my mistake(i already knew about that) but i need the solution code. i cant figure it out, i am absolutely new to coding. can you make changes to my code so my problem can be solved. i have column in the DB for ID (its 01, 02, 03 likewise for each entry) how do i use that to find correct clicked item?
 

stevel05

Expert
Licensed User
Longtime User
Have a look at AddTwoLinesAndBitmap2 you can add the ID to and get it returned from the listview.

If that doesn't help, you'll need to post some more code or preferably, zip and post the project so we can see exactly how you are trying to make it work, there is not enough code there to get a tested, working response.
 
Last edited:

electronik54

Member
Licensed User
Longtime User
You should put these lines out of the loop.
The layout definitions are the same for all entries.
B4X:
FA_s_lv.ScrollingBackgroundColor = Colors.White
FA_s_lv.TwoLinesAndBitmap.Label.TextSize = 15
FA_s_lv.TwoLinesAndBitmap.Label.TextColor = Colors.Black
FA_s_lv.TwoLinesAndBitmap.SecondLabel.TextSize = 13
FA_s_lv.TwoLinesAndBitmap.SecondLabel.TextColor = Colors.DarkGray
What are Position and Findrow supposed to do ?
You are doing nothing with them in the routine.

The code should work OK, what do you expect.
If you click on an item in the ListView what do you get ?

Best regards.


when i search the DB for certain items it returns me the list of those items. now when i click on the first item of the searched list(which can be any where in the DB) it displays info about the first item in the DB(when its supposed to show info about the clicked item in searched list).i know why this i happening, what i want is the solution to the problem.
 

klaus

Expert
Licensed User
Longtime User
Not sure I understand exactly your problem ?
Sorry, but with just a few code snippets and no precise explanation it's very difficult to help.
Post a small project showing the problem (or the whole project if it's not too big) as a zip file so we could test it in the same conditions as you do otherwise we need to guess what you could have done wrong and we are turning in circle.

Best regards.
 

luke2012

Well-Known Member
Licensed User
Longtime User
The ListView control is a very powerful control. It allows you to show short or long lists in a very "sleek" way.

Creating a simple list is easy:
B4X:
Sub Globals
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ListView1.Initialize("ListView1")
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
    Next
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Activity.Title = Value
End Sub
lv_single.png


The ListView can be added programmatically or with the designer. For now the items must be added with code.
About the code:
- ListView1.Initialize("ListView1") - Here we initialize the list and set the event name property to ListView1. Which means that in order to catch related events we should have subs like: ListView1_ItemClick.
- ListView1.AddSingleLine - adds a single line item.
- Activity.AddView(ListView1, 0, 0, 100%x, 100%y) - Note the use of the percentage units. We are setting the width and height to the values of the containing activity.

There are currently three types of items: single line, two lines and two lines and bitmap.
Each type can be customized. The default look is:

lv_1.png


This is the relevant code:
B4X:
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize(File.DirAssets, "button.gif")
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
        ListView1.AddTwoLines("Item #" & i, "This is the second line.")
        ListView1.AddTwoLinesAndBitmap("Item #" & i, "This is the second line.", Bitmap1)     
    Next
We can set different bitmaps to different items. Note that this code loads an image file named button.gif. This file should be added to the Files tab (in the right pane). You can download the project which is attached to this post.

Customizing each type
Each of the three types can be customized. The change will affect all items of that type.
The ListView has three "models" which are stored under:
- SingleLineLayout
- TwoLinesLayout
- TwoLinesAndBitmap

Each of this model has an ItemHeight property, a Background property and one or more views properties. Again, if you change any of these properties it will affect all the items of this type.
Example of customizing the single line items:
B4X:
ListView1.SingleLineLayout.ItemHeight = 100dip
    ListView1.SingleLineLayout.Label.TextSize = 20
    ListView1.SingleLineLayout.Label.TextColor = Colors.Blue
    ListView1.SingleLineLayout.Label.Gravity = Gravity.CENTER
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
        ListView1.AddTwoLines("Item #" & i, "This is the second line.")
        ListView1.AddTwoLinesAndBitmap("Item #" & i, "This is the second line.", Bitmap1)     
    Next
Result:
lv_3.png


Note that the ItemHeight is set to 100dip. The 'dip' unit causes it to automatically scale the height based on the current device scale. For the TextSize it is a mistake to use dip units as the text size is already measured in scaled units.

The above code is equivalent to this code (which is a bit more clear):
B4X:
    ListView1.SingleLineLayout.ItemHeight = 100dip
    Dim label1 As Label
    label1 = ListView1.SingleLineLayout.Label 'set the label to the model label.
    label1.TextSize = 20
    label1.TextColor = Colors.Blue
    label1.Gravity = Gravity.CENTER
In a similar way you can change the way the other types look.
The other types have additional views: SecondLabel and ImageView.

Return value
First notice that there is no selected item. The reason is that the combination of scrolling the list with finger and scrolling with the wheel or keyboard makes it non relevant.
You should catch the ItemClick event and then handle the clicked item.
The value of the clicked item is passed as a parameter.
Now, what is a value of an item???
By default this is the text stored in the first line.
However you can change it to any object you like by using:
AddSingleLine2, AddTwoLines2 and AddTwoLinesAndBitmap2 methods. These methods receive an additional parameter which is the return value. This allows you to pass more information as required by your application.

Background optimization
There is a hidden assumption that the background behind the ListView is solid black. If you set the background to something else like a gradient background or image you will see that during scrolling the background disappears.
You can change the background scrolling color with the ScrollingBackgroundColor property. If the background is not solid color set it to Colors.Transparent.

Example (the activity background is a gradient):
B4X:
    Dim GD As GradientDrawable
    GD.Initialize("TR_BL", Array As Int(Colors.Gray, Colors.LightGray))
    Activity.Background = GD
    ListView1.ScrollingBackgroundColor = Colors.Transparent
Tips
If you want a single line item with a bitmap (and do not need two lines and a bitmap), you can set the visible property of the second label to false.

If you have many items then you should enable the fast scroller:
B4X:
ListView1.FastScrollEnabled = true
lv_2.png


A small example is available here: http://www.b4x.com/android/files/tutorials/ListView.zip

It's possible to have a SingleLine with bitmap ?
 

mitsusdev

Member
Licensed User
Longtime User
You will still need to add the items manually.
In the designer you should choose Tools - Generate members and choose the ListView. It will add a declaration for this view under Sub Globals. Note that you should not initialize or add the view to the activity. This is done automatically when you call Activity.LoadLayout.

i have a simple list defined in design, i must inizializate the list view for remove a runtime errror.
but list view don't show the items, why ?

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim ListView1 As ListView
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("Main")
   
    ListView1.Initialize("ListView1")
   
    ListView1.AddSingleLine("AAA")
    ListView1.AddSingleLine("BBB")
    ListView1.AddSingleLine("CCC")
End Sub
 

Orazio8909

Member
Licensed User
Longtime User
Hello guys,

I have recently joined the community of B4A, I'm creating a game and I would like to enter the objectives to unlock during the game, given that I searched on google and on the forum but did not find anything, I wanted to know if it was possible to add a progressbar in a listview in such a way as to have a small description and above and below have a progressbar that indicates the completion status of the lens itself. Thanks in advance to all those that I will respond.
 

Orazio8909

Member
Licensed User
Longtime User
I figured you could not do. thanks for your response. I'll try to take a look at the library Erel (CustomListView class).
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
when I place a bitmap, what are the standard dimensions?
How can I change them?
 

Kwame Twum

Active Member
Licensed User
Longtime User
I wonder how I'll be able to allow a user to delete an item from a listview upon long clicking and displaying a menu to them... any help?
 
Top