Android Tutorial ListView tutorial

Kwame Twum

Active Member
Licensed User
Longtime User
Declare a type in Process_Globals sub of any code or activity module
eg.
B4X:
Type MyView(row1 As String, row2 As String)

Then, on the ItemClick event of your listview, you assign the value of the item clicked to a "MyView" variable
eg.
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim all As MyView ' Declaring a variable 'all' as the type we created above in Process_Globals
    all = Value 'Assigning the value of the clicked item to our variable
End Sub
Then, to retrieve one of the two lines in the item, you call any of the following:
B4X:
Dim stt as String = all.row1 'This gets the 1st line in the item and assigns it to a String variable stt
Dim stt2 as String = all.row2 'This gets the 2nd line in the item and assigns it to a String variable stt2
I hope this will be of help.
 

luke2012

Well-Known Member
Licensed User
Longtime User
Declare a type in Process_Globals sub of any code or activity module
eg.
B4X:
Type MyView(row1 As String, row2 As String)

Then, on the ItemClick event of your listview, you assign the value of the item clicked to a "MyView" variable
eg.
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim all As MyView ' Declaring a variable 'all' as the type we created above in Process_Globals
    all = Value 'Assigning the value of the clicked item to our variable
End Sub
Then, to retrieve one of the two lines in the item, you call any of the following:
B4X:
Dim stt as String = all.row1 'This gets the 1st line in the item and assigns it to a String variable stt
Dim stt2 as String = all.row2 'This gets the 2nd line in the item and assigns it to a String variable stt2
I hope this will be of help.

Thanks 4 your reply!
This is could be one solution that can be implemented (It was better if the listview object allow to access the two lines handled by items)
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
how can I change the color property of the list view if you use AddTwoLines2?
thanks
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
I need to change The text color.
Thanks
 

Kwame Twum

Active Member
Licensed User
Longtime User
I need to change The text color.
Thanks
B4X:
'Say lv is your listview (with two lines)

lv.TwoLinesLayout.Label.TextColor = Colors.Gray '1st Line text color
lv.TwoLinesLayout.SecondLabel.TextColor = Colors.Red '2nd Line text color

' There are other properties as you'll realise.
Hope this helps.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
OK, but where can I find all members and property of the object? thanks
 

Kwame Twum

Active Member
Licensed User
Longtime User
OK, but where can I find all members and property of the object? thanks
Entering the name of a type, say - lv (being a listview), followed by a period(.) brings up all the properties and methods of that type.

See the attachment.
 

Attachments

  • members_and_properties.png
    members_and_properties.png
    5.9 KB · Views: 551

luke2012

Well-Known Member
Licensed User
Longtime User
Declare a type in Process_Globals sub of any code or activity module
eg.
B4X:
Type MyView(row1 As String, row2 As String)

Then, on the ItemClick event of your listview, you assign the value of the item clicked to a "MyView" variable
eg.
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim all As MyView ' Declaring a variable 'all' as the type we created above in Process_Globals
    all = Value 'Assigning the value of the clicked item to our variable
End Sub
Then, to retrieve one of the two lines in the item, you call any of the following:
B4X:
Dim stt as String = all.row1 'This gets the 1st line in the item and assigns it to a String variable stt
Dim stt2 as String = all.row2 'This gets the 2nd line in the item and assigns it to a String variable stt2
I hope this will be of help.

Hi @Kwame Twum, I'm testing your solution but I got a casting error (attacched) on the stament :

B4X:
Dim all As MyView ' Declaring a variable 'all' as the type we created above in Process_Globals
all = Value 'Assigning the value of the clicked item to our variable
 

Attachments

  • Screenshot_2014-04-08-09-14-53.png
    Screenshot_2014-04-08-09-14-53.png
    233.7 KB · Views: 578

luke2012

Well-Known Member
Licensed User
Longtime User
Where is Value defined ?
You need to post more code to see where the probelm is.

Hi @klaus,
The Value is defined by default event signature for the ListView's click event.

As soon as possible I post you more code.
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
In your post you show only two lines of code without the definition of Value therefore the question.
How do you fill the ListView ?

Now I don't have the source code on the machine where I'm writing the post (I post the code when I go back to home).

In few words the listview is populated with the AddTwoLinesAndBitmap method.
The first line is filled with text, the second line is filled with text or blank "" and the line is completed with a bitmap passed as AddTwoLinesAndBitmap parameter.
 

klaus

Expert
Licensed User
Longtime User
The first line is filled with text, the second line is filled with text or blank "" and the line is completed with a bitmap passed as AddTwoLinesAndBitmap parameter.
The problem is that you fill the ListView with strings.
So Value in the ListView1_ItemClick routine is the String of the first line and not a MyView type variable !
You should use AddTwoLinesAndBitmap2(Text1 As String, Text2 As String, Bitmap As Bitmap, ReturnValue As Object) to fill the ListView, where ReturnValue is the MyView type variable.
 

luke2012

Well-Known Member
Licensed User
Longtime User
The problem is that you fill the ListView with strings.
So Value in the ListView1_ItemClick routine is the String of the first line and not a MyView type variable !
You should use AddTwoLinesAndBitmap2(Text1 As String, Text2 As String, Bitmap As Bitmap, ReturnValue As Object) to fill the ListView, where ReturnValue is the MyView type variable.

Thanks @klaus for your explanation.
So passing MyView variable to AddTwoLinesAndBitmap2, according to the MyView variable, we can access the two text lines of the listview item. Correct ?
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Use something like this:
B4X:
Dim Val1 as MyView
Val1.row1 = TextRow1
Val1.row2 = TextRow2
ListView1.AddTwoLinesAndBitmap2(TextRow1, TextRow2, Bitmap, Val1)
And you can use the code from post #141:
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Dim all As MyView ' Declaring a variable 'all' as the type we created above in Process_Globals
    all = Value 'Assigning the value of the clicked item to our variable
End Sub
 

Augustinas Impalis

Member
Licensed User
Longtime User
Is there a way to lines and bitmap in this order:

Item #1
This is the second line.
Bitmap

and is there a way to enlarge the bitmap too so its clearer to see?
 

klaus

Expert
Licensed User
Longtime User
You can change the properties Left, Top, Width and Height of all three views in the ListView items.
ListView1.TwoLinesAndBitmap.Label.Top etc
ListView1.TwoLinesAndBitmap.SecondLabel.Top etc
ListView1.TwoLinesAndBitmap.ImageView.Top etc
Make sure that ListView1.TwoLinesAndBitmap.ItemHeight is set according to the heights of the internal views.

EDIT: Changed ListView1.TwoLinesAndBitmap.Bitmap.Top etc to ListView1.TwoLinesAndBitmap.ImageView.Top etc. see post # 161
 
Last edited:
Top