Selectedindex in Listview

ginsh

Member
Licensed User
Longtime User
hi,

anyone if there is a similar thing called SelectedIndex in Listview control similar to one in vb6 or .net for listbox. for e.g.

If Form1.ListBox1.SelectedIndex = 0 Then

some code........

end if

thanks
 

ginsh

Member
Licensed User
Longtime User
thanks for the reply.. but how can i use it in b4a?

i have 2 listviews, wen i click on the first listview items at any position, it should show the related items in second listview.

for e.g.
listview1 items:-
a
b
c
d
e
wen i click on say "a" it should show listview2 items as a1,a2,a3,...
if i click on "d" then in listview2 items should change to d1,d2,d3...

thanks..
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Something like this...
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
   Dim t
   
   t = ListView1.GetItem(Position)   
   Select Case t
      Case "a"
      '...
   End Select
End Sub

Rolf
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You have two possibilities check either Position or Value.
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    ListView2.Clear
    Select Value
    Case "a"
        ListView2.Add("a1")
        ListView2.Add("a2")
       '...
     Case "b"
        ListView2.Add("b1")
        ListView2.Add("b2")
       '...   
    End Select
End Sub
or
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    ListView2.Clear
    Select Position 
    Case 0
        ListView2.Add("a1")
        ListView2.Add("a2")
        '...
    Case 1
        ListView2.Add("b1")
        ListView2.Add("b2")
        '...
    End Select
End Sub
@Rolf, Be careful it's Select Value and not Select Case Value like in Visual Basic.

Best regards.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Thanks Klaus,

that's the problem when you are dealing with different Basic dialects. 80% I am dealing with PowerBasic and VB.

I remember I had a similar problems in the old days when I sometimes mixed up QuickBasic and dBase keywords.

Rolf
 
Upvote 0

ginsh

Member
Licensed User
Longtime User
Hi Klaus,

Thanks for the help... i need one more help...


i have say 4 buttons..1,2,3,4

and have 2 listviews,
wen i click on each buttons the items in the listview1 should change..for e.g...on the click of say 1.button the items should change in list1 to a1,b1,c1...wen 2 is pressed a2,b2...

wen i click on the first list1 items at any position, it should show the related items in second list2.

for e.g.
listview1 items on button click 1-
a1
b1
c1
d1
e1
wen i click on say "a1" it should show list2 items as a11,a12,a13,...
if i click on "d" then in list2 items should change to d11,d12,d13...

Please let me know if i should put code under button click or listview..
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
@Klaus:

I just checked checked the code of two of my projects and noticed that I am always using "Select Case" instead of "Select", and it works. I guess Erel had the foresight that this would happen and tolerates or corrects it when the code gets translated to Java.

Nether the less I will correct my code of course.

Rolf
 
Upvote 0

ginsh

Member
Licensed User
Longtime User
Hi Klaus,

Thanks for the help... i need one more help...


i have say 4 buttons..1,2,3,4

and have 2 listviews,
wen i click on each buttons the items in the listview1 should change..for e.g...on the click of say 1.button the items should change in list1 to a1,b1,c1...wen 2 is pressed a2,b2...

wen i click on the first list1 items at any position, it should show the related items in second list2.

for e.g.
listview1 items on button click 1-
a1
b1
c1
d1
e1
wen i click on say "a1" it should show list2 items as a11,a12,a13,...
if i click on "d" then in list2 items should change to d11,d12,d13...

Please let me know if i should put code under button click or listview..

please any one..help me..
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
@rbsoft
Interesting to know. I remember the difference already from Basic4PPC and changed my mind at that time. If I once would need to code in VB I surely would make the inverse mistake.

@ginsh
You need to put the code to fill ListView1 in the Button_Click event routines and put the code to fill ListView2 in the ListView1_ItemClick event routine.
You could set the Click event for all buttons to one routine and use the Tag property to know what button raised the event.
Depending on how the values are defined it could be interesting to use a three dimension array to save the values.
Values(i, j, k)
Dimesion i for a12, b23 etc.
Dimesion j for a12, b23 etc.
Dimesion k for a12, b23 etc.
In that way you could easily use the indexes to fill the ListViews.

Best regards.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
@rbsoft
Interesting to know. I remember the difference already from Basic4PPC and changed my mind at that time. If I once would need to code in VB I surely would make the inverse mistake.

@ginsh
You need to put the code to fill ListView1 in the Button_Click event routines and put the code to fill ListView2 in the ListView1_ItemClick event routine.
You could set the Click event for all buttons to one routine and use the Tag property to know what button raised the event.
Depending on how the values are defined it could be interesting to use a three dimension array to save the values.
Values(i, j, k)
Dimesion i for a12, b23 etc.
Dimesion j for a12, b23 etc.
Dimesion k for a12, b23 etc.
In that way you could easily use the indexes to fill the ListViews.

Best regards.

While a 3d area is certainly an option, in a similar case I used a 1d, formatted as 'id','subid','description', then quering my list for items with id=subid. But it's just a matter of taste, especially in small areas :)
 
Upvote 0

ginsh

Member
Licensed User
Longtime User
hi klaus,

I found a simple way.. hope its correct..
B4X:
Sub Globals
Dim Data as sting
End sub

under each button click routine set differet values for Data varible.

B4X:
Sub ListView2_ItemClick (Position As Int, value As Object)
ListView1.Clear    

If Data = "A" Then 
Select Position     
Case 0        
ListView1.AddSingleLine("a1")        
ListView1.AddSingleLine("a2")            
Case 1        
ListView1.AddSingleLine("a3")        
ListView1.AddSingleLine("a4")            
End Select

End If

If Data = "B" Then
Select Position     
Case 0        
ListView1.AddSingleLine("D1")        
ListView1.AddSingleLine("D2")            
Case 1        
ListView1.AddSingleLine("D3")        
ListView1.AddSingleLine("D4")            
End Select

End If


this helps in finding out which button was clicked...its working fine now...if anything wrong in doing this let me know...thanks a lot for ur reply and help
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
B4X:
Dim Data as sting

should be

B4X:
Dim Data as String

Rolf
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I don't understand anymore what you want to do.
Your explanations in post#8 and post#17 are not the same.
Make your statements more precise on what you want to do before trying to find how to do it.
It would probably be easier to understand with concrete data.
As long as we don't know what exactly you want to do it's almost impossible to give good advices.

Best regards.
 
Upvote 0
Top