Android Tutorial listview reading second line

I dont know how to get the second line returned from a listview

help is appreciated

reading from the manual:

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.

B4X:
Sub foutView_ItemClick(Position As Int, Value As Object)
toastmessageshow(value)

end sub
 

Helihead

Member
Licensed User
Longtime User
OK so I have a much stripped down version that exhibits bad behavior. It isn't exactly the same behavior as I get in my main app originally but it is still wrong.

In my original problem it always returned the correct position but the wrong value and the value changed. In this example it always returns the correct position and always returns the Value for the last item I loaded in the listView which is still bad behavior.

I zipped the complete example to attach but it is too large to attach. I will send it to you directly if you can tell me how. I have attached a png of the screenshot of the listview sp you can see the colors loaded correctly.

I tried it logging Value directly, assigning Value to lvd and declaring lvd2 as ListViewData locally and assigning Value to that. The overall problem is that it returns correct Position but incorrect value for the Position selected.

If I walk down the listView clicking each item in turn, the log output is as follows:

Position = 0
Selected = [FirstRow=Green, SecondRow=Info about Green, Pic=(Bitmap): 200x192, IsInitialized=false]
Position = 1
Selected = [FirstRow=Green, SecondRow=Info about Green, Pic=(Bitmap): 200x192, IsInitialized=false]
Position = 2
Selected = [FirstRow=Green, SecondRow=Info about Green, Pic=(Bitmap): 200x192, IsInitialized=false]
Position = 3
Selected = [FirstRow=Green, SecondRow=Info about Green, Pic=(Bitmap): 200x192, IsInitialized=false]
Position = 4
Selected = [FirstRow=Green, SecondRow=Info about Green, Pic=(Bitmap): 200x192, IsInitialized=false]

The app code is listed here:
'Activity module
Sub Process_Globals

End Sub

Sub Globals
Dim listPlan As ListView
Type ListViewData (FirstRow As String, SecondRow As String, Pic As Bitmap)
Dim lvd As ListViewData
Dim bmPlanned As Bitmap
bmPlanned.Initialize(File.DirAssets, "bmPlanned.jpg")
listPlan.Initialize("")
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim Lb As Label

Activity.LoadLayout("plan")
listPlan.BringToFront
listPlan.TwoLinesAndBitmap.SecondLabel.Visible = True
listPlan.TwoLinesAndBitmap.Label.Visible = True
listPlan.ScrollingBackgroundColor = Colors.Transparent
Lb = listPlan.TwoLinesAndBitmap.Label
Lb.Textsize = 16
Lb.TextColor = Colors.Black
Lb.Color = Colors.White

lvd.FirstRow = "Red"
lvd.SecondRow = "Info about Red"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
lvd.FirstRow = "Blue"
lvd.SecondRow = "Info about Blue"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
lvd.FirstRow = "Yellow"
lvd.SecondRow = "Info about Yellow"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
lvd.FirstRow = "Orange"
lvd.SecondRow = "Info about Orange"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
lvd.FirstRow = "Green"
lvd.SecondRow = "Info about Green"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub listPlan_ItemClick(Position As Int, Value As Object)
Log("Position = " & Position)
Log("Selected = " & Value)
End Sub
 

Attachments

  • screenshot.png
    screenshot.png
    32.2 KB · Views: 504

klaus

Expert
Licensed User
Longtime User
You need to Dim and Initialize the type variable for each entry in the ListView.
B4X:
Dim lvd As ListViewData
lvd.Initialize
lvd.FirstRow = "Red"
lvd.SecondRow = "Info about Red"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)

Dim lvd As ListViewData
lvd.Initialize
lvd.FirstRow = "Blue"
lvd.SecondRow = "Info about Blue"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
.
.
A Type variable is an object that means if you don't dim and initialize a new one you add always the same object to each entry and the values are the last you entered for all entries.

Best regards.
 

Helihead

Member
Licensed User
Longtime User
You need to Dim and Initialize the type variable for each entry in the ListView.
B4X:
Dim lvd As ListViewData

.
A Type variable is an object that means if you don't dim and initialize a new one you add always the same object to each entry and the values are the last you entered for all entries.

Best regards.


Not to be argumentative but as you can see in the screen shot I originally attached, I am not re-dimming it and it does not re-add the same object to each entry. I dim it once, change the values 5 times and it successfully adds different values to the listView each time. The listView looks correct after it is loaded. The problem is when it pulls the Value on an itemClick.

That does seem like a valid workaround as I tried redimming it each time and it and it does work. Still seems there is a bug in the listView itemclick cause it should always pull the value for what you clicked not the last thing you loaded.

OK that has me unstuck. Thanks for your help and patience.
 

klaus

Expert
Licensed User
Longtime User
It's not a bug, that how it works when you use a type object for the return value.
When you set the text of the ListView.Label.Text properties the value is OK because you set a String variable that is not linked to the type object.

Best regards.
 

Helihead

Member
Licensed User
Longtime User
Understood. Made the change in my main app and the problem appears to be solved.

I'm new to android and b4A but I have been coding 35 years and this is probably the most stable language and dev environment overall that I have ever seen.

Thanks again for your help.
 
Top