Android Tutorial listview reading second line

Cor

Active Member
Licensed User
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
 

Erel

Administrator
Staff member
Licensed User
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
 

NeoTechni

Well-Known Member
Licensed User
How would you declare the type if there was a picture?
And can you obtain the Value using GetItem somehow, cause it's not working for me. I get an exception about strings

Just:
Type ListViewData (FirstRow As String, SecondRow As String, Picture as Bitmap)
?
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Your type looks fine. How do you use it and where do you get the error?
 

TDowdeswell

Member
Licensed User
AddTwoLinesandbitmap2 with Type

Erel,

Re: the above example I love this feature but am having problems in that no matter which item I select in my list 'Position' is returned correctly but 'Value' always contains the value of the last item in the list.

The list of messages is displayed perfectly.

Sub Globals
Type ListViewData (FirstRow As String, SecondRow As String, Pic As Bitmap)

Sub Display_All_Messages

For i = 0 To List1.Size - 1
lvd.Initialize
lvd.FirstRow=gblPersonName & strDateTime
lvd.SecondRow=Sms.Body
lvd.Pic=gblPhoto
listInbox.AddTwoLinesandbitmap2(lvd.FirstRow,lvd.SecondRow,lvd.pic, lvd)
Next
etc...

Sub ListInbox_ItemClick (Position As Int, Value As Object)
'Get the details and fill them in on the second screen

Dim lvd As ListViewData

'Disable Fields on Base screen
listinbox.Enabled=False
lvd = Value
strMessage=lvd.secondrow
sendNew_Click

End Sub

Your help would be much appreciated, as always.


Terry
 

klaus

Expert
Licensed User
You must also add a Dim here:
B4X:
Sub Display_All_Messages 
 
For i = 0 To List1.Size - 1
Dim lvd As ListViewData
lvd.Initialize
Best regards.
 

TDowdeswell

Member
Licensed User
Klaus,

Yes, that's it and thank you for that.

Dim obviously behaves differently in B4A to other BASICs that I know.

I had declared the object further up the procedure along with all my other declarations as per the way I was taught (many, many years ago!) rather than re-declaring it every time round the loop.

I'll now try to get used to this and thanks again,


Terry
 

Helihead

Member
Licensed User
OK so this works well for me if I am in an ItemClick that gives me Value As Object.

What I need to do is be able to programmatically walk down the listview and read first line, second line and bitmap from each object in turn. Instead of using VALUE to pass the object to lvd, I tried to modify your code to have GetItem pass the object to lvd.

lv is already setup as my listview with a list of items setup as 2 lines and a bitmap.

firstLine, secondLine and myBmp are already Dimmed properly.

For i = o To listview.size -1
lvd = lv.GetItem(i) '<<< IT THROWS A STRING ERROR HERE
firstLine = lvd.FirstRow
secondLine = lvd.SecondRow
myBmp = lvd.Pic
Next

I can get the first line the same way you would if was a single line but not the second or bitmap.

Is there anyway to walk down a listview in a For Next, select each in turn, read the first line, second line and bitmap from each into variables?

Thanks for any help
Jeff
 

Helihead

Member
Licensed User
Yep:

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

kickaha

Well-Known Member
Licensed User
It seems to be responding as if a return value has not been set
GetItem (Index As Int) As Object

Returns the value of the item at the specified position.
Returns the "return value" if it was set and if not returns the text of the first line.
You can check this in your code by looking at what lv.GetItem(I) is returning as a string.
 

Helihead

Member
Licensed User
It seems to be responding as if a return value has not been set


You can check this in your code by looking at what lv.GetItem(I) is returning as a string.

That line throws java.lang.ClassCastException: java.lang.String while trying to execute. I'm stepping through with the debugger but so far no luck getting that line to execute.
 

Helihead

Member
Licensed User
That line throws java.lang.ClassCastException: java.lang.String while trying to execute. I'm stepping through with the debugger but so far no luck getting that line to execute.
My error. I was missing the return lvd in one of my add line statements.

Now it runs with no errors but no values returned for first line and second line.
 
Last edited:

bagstoper

New Member
Licensed User
Resolved??

Did you ever get it to read your second line? i am in need of a solution to a the same problem that's why i am asking.
 

Erel

Administrator
Staff member
Licensed User
I see. You can always use an additional Map or List and store whatever you need.
 

walterf25

Well-Known Member
Licensed User
Listview second label

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?

schedule_lstschedule_itemclick (B4A line: 313)


lvd = Value

java.lang.ClassCastException: java.lang.String
at com.dandre.apps.schedule._lstschedule_itemclick(schedule.java:820)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:136)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:124)
at anywheresoftware.b4a.objects.ListViewWrapper$1.onItemClick(ListViewWrapper.java:55)
at android.widget.AdapterView.performItemClick(AdapterView.java:290)
at android.widget.ListView.performItemClick(ListView.java:3605)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1838)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)


at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: java.lang.String
I declared the type just as in the example .

Thanks,
Water
 
Top