Listview Click Event?

disit10

Member
Hey everyone im a little confused on how to go about do this with b4a. I can pull this off in visual basic just fine. Basically how do you go about opening one activity with a listview click? They all use this one activity for the same layout and such, the main thing I am wanting to do is when a certain listview item is clicked it will change the text and imageview image to whatever it corrospondes with in the listview. I am lost. An example would be lets say i clicked on item #1 it loads up activity A which has an imageview and some text. Now lets say I clicked on Item #2 and loaded the same Activity A it is still going to show the same imageview and text. How can I change the imageview and text when a certain listview item is clicked?
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
I think you should declare in the process_global sub, the variables you want to change when you click on the list. then, in the activity_create of the other activity, you can easily get the values of these variables and set your textboxes and imageviews accordingly.
 
Upvote 0

disit10

Member
I think you should declare in the process_global sub, the variables you want to change when you click on the list. then, in the activity_create of the other activity, you can easily get the values of these variables and set your textboxes and imageviews accordingly.

I already declared the values in the Sub Globals. I have already tried setting the values of the textboxes and such but have no idea how to. Like I said I can easily achieve this with vb. An example would be nice.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I already declared the values in the Sub Globals. I have already tried setting the values of the textboxes and such but have no idea how to. Like I said I can easily achieve this with vb. An example would be nice.
ok, say we have a process_global variable 'sometext'.
in listview_click, we get the position of the clicked item.
suppose we have an array of strings, name it mystrings(). these should hold the values we want to feed our next activity's textboxes.
we set sometext=mystrings(position):startactivity(your next activity)
in the activity_create of your next activity, write for example:
edittext1.text=main.sometext
where 'main' is the name of your first activity.
 
Upvote 0

disit10

Member
ok, say we have a process_global variable 'sometext'.
in listview_click, we get the position of the clicked item.
suppose we have an array of strings, name it mystrings(). these should hold the values we want to feed our next activity's textboxes.
we set sometext=mystrings(position):startactivity(your next activity)
in the activity_create of your next activity, write for example:
edittext1.text=main.sometext
where 'main' is the name of your first activity.

Still is not making sense to me this is the code I am using for each listview item to get its value.

B4X:
If Value = "Item 1" Then
   Activity.LoadLayout("BulbLayout")
   End If
 
Upvote 0

disit10

Member
To make it easier here is the code im using.

This one is for getting the value of the listview items.

B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
   If Value = "Item 1" Then
   Activity.LoadLayout("BulbLayout")
   End If

        If Value = "Item 2" Then
   Activity.LoadLayout("BulbLayout")
   End If
End Sub

This is what I have declared in the Sub Globals.

B4X:
Sub Globals
        Dim ListView1 As ListView
   Dim ImageView1 As ImageView
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Still is not making sense to me this is the code I am using for each listview item to get its value.

B4X:
If Value = "Item 1" Then
   Activity.LoadLayout("BulbLayout")
   End If

This is not a new activity, it's the same activity with a new layout.
Since you use the same activity, I don't see why you cannot set the textboxes you have, or imageviews with the appropriate values. Perhaps I don't understand something here, I don't know. Furthemore, though it's your choice, I would choose to create a new activity, instead of loading a new layout. Just to have coding for each module more clear.
 
Upvote 0

disit10

Member
This is not a new activity, it's the same activity with a new layout.
Since you use the same activity, I don't see why you cannot set the textboxes you have, or imageviews with the appropriate values. Perhaps I don't understand something here, I don't know. Furthemore, though it's your choice, I would choose to create a new activity, instead of loading a new layout. Just to have coding for each module more clear.

Yeah it would be easier to create a new activity for each listview Item but considering I have 652 would this slow down any part of the program doing it this way?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a small test program according to mc73s' suggestions.
Main activity with the ListView.
Texsts and image references in arrays.
A second Activity that shows the text and the image according to the ListView selected item.
Main activity:
B4X:
Sub Process_Globals
    Dim NumberOfImages As Int        : NumberOfImages = 4
    Dim Texts(NumberOfImages) As String
    Dim Images(NumberOfImages) As String
    Dim ListIndex As Int
End Sub

Sub Globals
    Dim ltvTest As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    InitTexts
End Sub

Sub Activity_Resume
    InitListView
End Sub

Sub ltvTest_ItemClick (Position As Int, Value As Object)
    ListIndex = Position
    StartActivity(Image)
End Sub

Sub InitTexts
    Dim i As Int
    
    Texts(0) = "Rose"
    Texts(1) = "Lys Martagon"
    Texts(2) = "Edelweiss"
    Texts(3) = "Gentian"
    For i = 0 To NumberOfImages - 1
        Images(i) = "Image" & i & ".jpg"
    Next
End Sub

Sub InitListView
    Dim i As Int
    
    ltvTest.Clear
    For i = 0 To NumberOfImages - 1
        ltvTest.AddSingleLine(Texts(i))
    Next
End Sub
Image activity:
B4X:
Sub Globals
    Dim lblTitle As Label
    Dim imvTest As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Image")
    lblTitle.Text = Main.Texts(Main.ListIndex)
    imvTest.Bitmap = LoadBitmap(File.DirAssets, Main.Images(Main.ListIndex))
End Sub
Best regards.
 

Attachments

  • ListViewTest.zip
    188.1 KB · Views: 536
Upvote 0

roarnold

Active Member
Licensed User
Longtime User
Sub ListView_Click

Evening,

I am building a dynamic list initially that can be 10 devices or 300 devices and I set it up to scroll. This also set the Sub ListView_Click (Position As int, Value As Object).

When I click on one of the items in the first list I want to bring up the rest of the associated information with that item which i can pull from the array. I have set ListView2 in designer and added button across the bottom that will alter so specifics shown in the device item contents.

What I am getting is the first list view of all the json content organized in an array. When I enter access information it presents all the information associated with that customer in the ListView1, and it scroll, as required.

When I click on the first item for example, it presents the second ListView(2) with header and buttons but no data about the device.

Any Items?


Sub ListView1_ItemClick (Position As Int, Value As Object)
Activity.LoadLayout("devices")
Dim ListView2 As ListView
ListView2.Initialize("ListView2")
Dim t
t = ListView1.GetItem(Position)
Select Case t
Case 0
ListView2.AddSingleLine("ListView2")

End Select
Dim Arm As Button
Dim Disarm As Button
Dim Status As Button
Dim Locate As Button
Dim Page As Button
Dim devices As ListView
End Sub


I would appreciate any pointers as to what I am doing wrong.

Thanks In Advance,
Ron
:BangHead:
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
I don't see where you are populating listview2's data? Right now you are just adding a single blank line to it
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Is listView2 created using the designer? If so, I think you should remove the 'dim listView2' command (dimming should be performed in your global sub, otherwise you're dimming a new object). If not, you should use the setLayout function, in order to position your new listview where you want it to appear.
 
Upvote 0

gapi

Active Member
Licensed User
Longtime User
Hi all, how can read listview rows data on click ? After DBUtils function for adding recordset to listview, I wrote this:

B4X:
Sub ListView1_ItemClick(Position As Int, Value As Object)
MyT.ToastMessageShow2(Value,  2,  50, 50, "bup.png", Colors.Black, Colors.Gray, 20, True)
End Sub

... it return me "[Ljava.lang.String;@463bce0" :sign0161: what is this value ?!?!
 
Upvote 0

iTzCorky

Member
Licensed User
Longtime User
Hi all, how can read listview rows data on click ? After DBUtils function for adding recordset to listview, I wrote this:

B4X:
Sub ListView1_ItemClick(Position As Int, Value As Object)
MyT.ToastMessageShow2(Value,  2,  50, 50, "bup.png", Colors.Black, Colors.Gray, 20, True)
End Sub

... it return me "[Ljava.lang.String;@463bce0" :sign0161: what is this value ?!?!

Post a new thread and ask this question and I will help you out.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Hi all, how can read listview rows data on click ? After DBUtils function for adding recordset to listview, I wrote this:

B4X:
Sub ListView1_ItemClick(Position As Int, Value As Object)
MyT.ToastMessageShow2(Value,  2,  50, 50, "bup.png", Colors.Black, Colors.Gray, 20, True)
End Sub

... it return me "[Ljava.lang.String;@463bce0" :sign0161: what is this value ?!?!


You are inputting a complex object for Value. Maybe you are trying to pass the Cursor object itself. You have to use GetString for the columns and pass this to the Value argument. Then you will be able to take the correct value.

Sent from my HTC Desire using Tapatalk
 
Upvote 0
Top