Android Question How to access item (SingleLine, TwoLines, TwoLinesBitmap) from ListView?

Diceman

Active Member
Licensed User
I want to write a sub that will traverse a ListView and I need to be able to access the items in it. I don't know ahead of time how the rows were added. Let's say it used:

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

which is from the ListView tutorial https://www.b4x.com/android/forum/threads/listview-tutorial.6537/

Ok, now I have to traverse it and access the items. How do I know item#10 is TwoLines or SingleLine or TwoLinesAndBitmap?
Here is the loop where I need to access the items in the ListView.

B4X:
For i=0 To aListView.Size-1
  Private locObj As Object
  locObj = aListView.GetItem(i)
  Log(GetType(locObj))     'Displays as JavaString
  'How do I determine if locObj has 1 label, 2 labels or 2 labels and a bitmap? And how do I access them in this loop?
next

TIA
 

Diceman

Active Member
Licensed User
Here is a better explanation of what I am trying to do.

Public Sub ResizeListView(aListView as ListView, aMinRows as Int, aExtraWidth as Int)

This sub will be used in other applications and unfortunately the Sub has no idea of how the ListView was created so I can't rely on AddSingleLine2() being used.

The purpose of the Sub is to resize the width of the Listview in order to display the text properly. It will also ensure that at least aMinRows are visible in the Listview. The Listview is being used as a popup menu and the text has to be readable and the Listview should only be wide enough to display the text and no wider.

I can use the code below to get the layout's Typeface and font size of the label(s) and size of the Bitmap:
B4X:
Private locLabel1, locLabel2 As Label
Private locBM As Bitmap
locLabel1 = aListView.SingleLineLayout.Label
locLabel2 = aListView.TwoLinesLayout.SecondLabel
locBM     = aListView.TwoLinesAndBitmap.ImageView

but I still need to know which layout was used when traversing through the items, so I can retrieve the appropriate Label1.text, Label2.Text or the Bitmap so I can determine the text lengths and resize the width of the ListView if necessary using code similar to that shown below:

B4X:
for i=0 to aListView.Size-1
  locWidth = CVS.MeasureStringWidth(aListView.GetItem(I), locLabel.TypeFace, locLabel.TextSize)  + aExtraWidth
  If locWidth > locMaxWidth Then
    locMaxWidth = locWidth
  End If
  'How do I retrieve Label2 text? Or the bitmap?
Next

But I don't see any way to access the 2nd label info or the bitmap from items in the Listview.
Am I missing something or is Listview that limited?

TIA
 
Upvote 0

KZero

Active Member
Licensed User
Longtime User
the standard listview is limited you should use Customlistview instead

 
Upvote 0

Diceman

Active Member
Licensed User
Ok, I will use CustomListView to create the popup menu. I've used it extensively in the past, but thought it was like using a sledgehammer to swat a fly. I was hoping I could get by with a simple ListView for displaying a popup menu.

Thanks again for the help.
 
Upvote 0

Diceman

Active Member
Licensed User
The more I think about it, the more attractive clv becomes. I will write a class around the clv to make it easier for generating popup menus.
Thanks for pointing dragging me in the right direction. :rolleyes:
 
Upvote 0

KZero

Active Member
Licensed User
Longtime User
The more I think about it, the more attractive clv becomes. I will write a class around the clv to make it easier for generating popup menus.
Thanks for pointing dragging me in the right direction. :rolleyes:
there is many ready to use popup menus you can check if it's suitable for you

I prefer this one
 
Upvote 0
Top