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
 

McJaegs

Member
Licensed User
Longtime User
Hi all, i really need to update one of my apps, and i'm trying to get the string from the second row in a listview, i'm following the example given here, but everytime i click on an item in the listview i get this error, can anyone please help me figure out why?



I declared the type just as in the example .

Thanks,
Water

I get this same error and the Type is ListViewData. Any help would be appreciated.

ListViewData is declared as

Type ListViewData (FirstRow As String, SecondRow As String)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think that you are confusing two examples.
DBUtils.ExecuteListView sets the value to be an array with the records.

See the example in DBUtils thread:
B4X:
Sub lstFailedTest_ItemClick (Position As Int, Value As Object)
   'Value is an array of strings
   Dim values() As String
   values = Value
   Dim testName As String
   testName = values(0)
   'find the index of this test in spnrTests and set it.
   For i = 0 To spnrTests.Size - 1
      If testName = spnrTests.GetItem(i) Then
         spnrTests.SelectedIndex = i
         spnrTests_ItemClick(i, spnrTests.GetItem(i))
         Exit
      End If
   Next
   txtGrade.SelectAll
End Sub
 

nK0de

New Member
multiple lines in a ListView item

If you only need the second line then you can add its text as the return value.
If you need both then you create a Type that includes two string values and then add objects of this type:
B4X:
Sub Globals
 Type ListViewData (FirstRow As String, SecondRow As String)
End Sub

For i = 0 to 100
 Dim lvd As ListViewData
 lvd.Initialize
 lvd.FirstRow = "abc"
 lvd.SecondRow=  "def"
 ListView1.AddTwoLines2(lvd.FirstRow, lvd.SecondRow, lvd)
Next


Sub ListView1_ItemClick(Position As Int, Value As Object)
 Dim lvd As ListViewData
 lvd = Value
 ...
End Sub

How can I display more say, 3 or 4 lines in a single ListView item? It won't be possible with the AddTwoLines2 method, right?
 

mc73

Well-Known Member
Licensed User
Longtime User
How can I display more say, 3 or 4 lines in a single ListView item? It won't be possible with the AddTwoLines2 method, right?
No, but if you simply need to do this in order to process a 3-4-N lines' item, by clicking, why not adding these separate lines and then clicking on any of them and present data?

For e.g. you can try something like this:

B4X:
ListView1.Clear 
' 5 lines per item
For i=0 To 9
For j=0 To 4
Dim tempLine As String 
tempLine="Line " & (j+1) & " of item " & (i+1)
ListView1.AddSingleLine (tempLine)
Next
Next

And then add in the ListView1_ItemClick event, this:

B4X:
Dim currentItem As Int 
   currentItem=Position/5: ' 5 is the number of lines per item
   Dim tempReturn As String 
   tempReturn=""
   For j=0 To 4
   tempReturn=tempReturn & ListView1.GetItem (currentItem*5+j) & Chr(10)
   Next
   Msgbox(tempReturn,"item " & (currentItem+1))

Of course, in case you need highlighted items, Erel's advice is the one you should follow.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
If all 3-4 don't need formatted different or you just need 3-4 wrapped lines like a paragraph you can expand the size of the internal labels and listview item. I have a listview containing a bold item at the top of each listview item and a 3-4 line definition below that all in one listview item/cell. I even indented the 2nd label and set its width to -1 to improve the look and make it fill width properly.
 

Helihead

Member
Licensed User
Longtime User
Hi Erel,

I have listViews in my program that use this approach almost verbatim except the names of the controls.

One works perfectly.

When I click the second listView, it tells me the correct position but it returns the wrong value.

For example: I have 5 items in my listview

0) lvd.FirstRow = "FirstItem", lvd.SecondRow = "First Item Details"
1) lvd.FirstRow = "SecondItem", lvd.SecondRow = "Second Item Details"
2) lvd.FirstRow = "ThirdItem", lvd.SecondRow = "Third Item Details"
3) lvd.FirstRow = "ForthItem", lvd.SecondRow = "Forth Item Details"
4) lvd.FirstRow = "FifthItem", lvd.SecondRow = "Fifth Item Details"

Let's say I click on the 2nd row (item 1). It tells me position = 1, Value = [FirstRow = "ForthItem", SecondRow = "Forth Item Details"]

if I check lvd.FirstRow and lvd.SecondRow it confirms the wrong value.

No matter what position I click, it tells me the correct position but the wrong value. The other listView is identical except the names and works correctly.

How can it know the position but not know the actual value of the row when it is clearly displayed where I click?




If you only need the second line then you can add its text as the return value.
If you need both then you create a Type that includes two string values and then add objects of this type:
B4X:
Sub Globals
 Type ListViewData (FirstRow As String, SecondRow As String)
End Sub

For i = 0 to 100
 Dim lvd As ListViewData
 lvd.Initialize
 lvd.FirstRow = "abc"
 lvd.SecondRow=  "def"
 ListView1.AddTwoLines2(lvd.FirstRow, lvd.SecondRow, lvd)
Next


Sub ListView1_ItemClick(Position As Int, Value As Object)
 Dim lvd As ListViewData
 lvd = Value
 ...
End Sub
 

Helihead

Member
Licensed User
Longtime User
The app is near 10K lines and kind of hard to slice it out of that.

The short question is how can a listView return the right position but the wrong value? It always returns one of the items in the displayed list but never returns the correct one.
 

Helihead

Member
Licensed User
Longtime User
This is the simple add...

lvd.FirstRow = "FirstItem"
lvd.SecondRow = "First Item Details"
lvd.Pic = bmPlanned
listPlan.AddTwoLinesAndBitmap2(lvd.FirstRow, lvd.SecondRow, lvd.Pic, lvd)
 

Helihead

Member
Licensed User
Longtime User
This is a snippet out of the itemclick...

Sub listPlan_ItemClick(Position As Int, Value As Object)
Dim selected As String
Dim lvd2 As ListViewData

lvd2 = Value
selected = Value
Log("Selected = " & selected)
Log("Firstrow = " & lvd2.FirstRow)
Log("Secondrow = " & lvd2.SecondRow)
Log("Clicked Grid Position = " & Position)

Position always returns the correct row clicked.

The value is always the correct first and second line values for one of the items displayed in the listview but is never the correct one for the position clicked.
 

Helihead

Member
Licensed User
Longtime User
Oh forgot to show the global type...

Type ListViewData (FirstRow As String, SecondRow As String, Pic As Bitmap)
Dim lvd As ListViewData
 

Helihead

Member
Licensed User
Longtime User
The problem is in the way you add the items. You probably use the same variable without "dimming" it again. See the common mistakes section here: Variables and Subs visibility

So I don't specifically see what you are talking about in that post but I understand well about global and private and think I understand what you mean.

ListViewData and lvd are both global and are never redimmed. lvd is used to add all items to all listviews in the app.

lvd2 is local/private and is only used for that itemclick to pull the value once I click.

As you can see though, selected = value and also logs out the incorrect value so lvd2 is a red herring at best.

If I Log("value = " & Value) it still logs the incorrect value.

If this was a case of the global lvd not being set correctly then I would not see the correct values show up in the listView to click but I do see a correct list displayed in the listView.

I click one and:

Log("Position = " & Position) prints the correct value for the listView position I click

Log("value = " & Value) prints the wrong value.

It always prints the correct position and always prints the value of SOME item displayed in the listView but never the correct one I click.

Another example would be if I load and SEE a list of colors in the listView such as Blue, Green, Red, Yellow, Orange in the listview to select from and I click Green I may get Position = 1 and Value = Orange as the logged value. No way Orange is at position 1. If it was then it would not display in position 4 like it does.

As I have said. I have this same basic code in several other places in the code and also in 2 other apps I am working on and it works fine. This is beginning to look like corruption in the language.
 

Helihead

Member
Licensed User
Longtime User
I am trying to cut down a much smaller example using code from my app that exhibits the wrong behavior and hopefully I am just doing something dumb.

The language has been rock solid all along so I'd bet on met doing something wrong.

Still this looks really really odd.

I'll try to post it soon so you can run and see what I am talking about.
 
Top