CustomListView - A flexible list based on ScrollView

Status
Not open for further replies.

ttsolution

Member
Licensed User
Longtime User
Dear all,
The below code return the View object at the line clicked in a ListView. How can we do the same with CustomListView? (ie., I mean how to return the view object at the line user tabed in a CustomListView). I need this view object to show a popup menu

B4X:
Sub GetViewAtPos(LV As ListView, Position As Int) As View
    Dim r As Reflector
    Dim v As View
    r.Target = LV
    Dim first As Int
    first = r.RunMethod("getFirstVisiblePosition")
    v = r.RunMethod2("getChildAt", Position - first, "java.lang.int")
    Return v
End Sub

Thanks for any help

Jonh,
 

eps

Expert
Licensed User
Longtime User
@ttsolution, really? all that code?? I just use :

B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)

    Detail_Display(Value)      

End Sub


After populating the ListView using

B4X:
    ListView1.AddTwoLines2(Cursor.GetString("name"), Cursor.GetString("display_date"), Cursor.GetInt("_id"))

Which pulls the information from a DB... and the last item entered is the Value when the item is clicked.. :) which as you can see from above that I use the value in another sub..

I'm not sure about CustomListView, but it should be possible to do something similar to the above code.

ETA : Ah, actually I forgot CustomListView is based on ScrollView - I used something like this for ScrollView..

B4X:
Sub lbl1_LongClick

    Dim Send As View

    Dim fetched_id, Obj As Int

    Send=Sender


    fetched_id=(Send.Tag)

    Dim NumberOfMatchedrows As Int

    NumberOfMatchedrows = SQL1.ExecQuerySingleResult("SELECT count(*) FROM favourite where fk_event_id = '" & fetched_id & "'")

End Sub
 

ttsolution

Member
Licensed User
Longtime User
@eps, thanks for your clarifies. But my case is different. I want to get the View object (this object actual is a panel) at the line that user have tabed on (not an Id). Why I need this ? because I want to show a popup menu right at the line that user tabed. I think Erel's suggestion may works, I will try and report back later.

Thank you.
 

Joni Van Roost

Member
Licensed User
Longtime User
Hi Erel,

Thanks for this Class, it really helps!
I have on problem though, I'm developing a friends-system and I need to have different buttons depending on the status of the friendship (for example if status = 1 then I need a "remove" button, if status = 2 then I need an "add" button,...).

So I need to add a different button depending on the status of the friendship. In my code the status is fetched from a sql tabel and saved in the ResponseID (int). I already checked the ResponseID values with "log(ResponseID) and they all seem correct.
The only problem is that the buttons are not showing up in the listview.

B4X:
Sub CreateListItem(Text As String,ID As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.LightGray
    'Dim b1,b2,b3 As Button
    'b1.Initialize("b1") 'all buttons click events will be handled with Sub Button_Click
    Dim lbl As Label
    Dim lblID As Label
    lblID.Initialize("")
    lblID.Text = ID
    lblID.Visible = False
    lbl.Initialize("")
    lbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    lbl.Text = Text
    lbl.TextSize = 16
    lbl.TextColor = Colors.White
 
    p.AddView(lbl, 5dip, 2dip, 150dip, Height - 4dip) 'view #0
    p.AddView(lblID, 5dip, 2dip, 150dip, Height - 4dip)
 
    If ResponseID = 0 Then

    Dim b1 As Button
    b1.Initialize("b1")
    b1.Color = Colors.RGB(77, 144, 254)
    b1.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
    b1.TextColor = Colors.White
    b1.Text = "Accept"
    p.AddView(b1, 155dip, 2dip, 150dip, 40dip) 'view #1
 
    Else If ResponseID = 1 Then
 
    Dim b2 As Button
    b2.Initialize("b2")
    b2.Color = Colors.RGB(77, 144, 254)
    b2.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
    b2.TextColor = Colors.White
    b2.Text = "Request Sent"
    p.AddView(b2, 155dip, 2dip, 150dip, 40dip) 'view #1
 
    Else If ResponseID = 2 Then
 
    Dim b3 As Button
    b3.Initialize("b3")
    b3.Color = Colors.RGB(244, 67, 54)
    b3.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
    b3.TextColor = Colors.White
    b3.Text = "Remove"
    p.AddView(b3, 155dip, 2dip, 150dip, 40dip) 'view #1
 
    End If
 
    'p.AddView(lbl, 5dip, 2dip, 150dip, Height - 4dip) 'view #0
    'p.AddView(lblID, 5dip, 2dip, 150dip, Height - 4dip)
    'p.AddView(b, 155dip, 2dip, 150dip, 40dip) 'view #1
 
    Return p
 
End Sub

I hope I explained it good enough, Thanks in advance.
 

cimperia

Active Member
Licensed User
Longtime User
The issue is probably with the datatype of RespondID. Try
B4X:
 IF RespondID = "0"
 
Last edited:

Joni Van Roost

Member
Licensed User
Longtime User
That's not the issue, I tried that already and it didn't help, as I said, the values are correct, I checked it by logging them aswel, it's something else.. I just can't seem to find the mistake in the code... :(
 

klaus

Expert
Licensed User
Longtime User
You could simplify your code:
B4X:
Dim btn As Button
btn.Initialize("btn")
btn.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
btn.TextColor = Colors.White
Select ResponseID
Case 0
   btn.Color = Colors.RGB(77, 144, 254)
   btn.Text = "Accept"
Case 1
   btn.Color = Colors.RGB(77, 144, 254)
   btn.Text = "Request Sent"
Case 2
   btn.Color = Colors.RGB(244, 67, 54)
   btn.Text = "Remove"
End Select
p.AddView(btn, 155dip, 2dip, 150dip, 40dip) 'view #1
 

Joni Van Roost

Member
Licensed User
Longtime User
You could simplify your code:
B4X:
Dim btn As Button
btn.Initialize("btn")
btn.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
btn.TextColor = Colors.White
Select ResponseID
Case 0
   btn.Color = Colors.RGB(77, 144, 254)
   btn.Text = "Accept"
Case 1
   btn.Color = Colors.RGB(77, 144, 254)
   btn.Text = "Request Sent"
Case 2
   btn.Color = Colors.RGB(244, 67, 54)
   btn.Text = "Remove"
End Select
p.AddView(btn, 155dip, 2dip, 150dip, 40dip) 'view #1
I thought about this too, but every button needs a different action, for example, when the remove button is clicked it will have to send a DELETE FROM query and if the accept button is clicked it will have to send an UPDATE query so I think it has to be like this so I can have different actions depending on the button.
 

cimperia

Active Member
Licensed User
Longtime User
I thought about this too, but every button needs a different action, for example, when the remove button is clicked it will have to send a DELETE FROM query and if the accept button is clicked it will have to send an UPDATE query so I think it has to be like this so I can have different actions depending on the button.
It makes no difference. Your btn_Click event can tell which button has been pressed based, for example, on the button's Text value. Sender is the object that you need to work with.
 
Last edited:

Geritt Schlag

Member
Licensed User
Longtime User
Hallo,
i'm use the CustomListview with many items from a database.

customlistview.jumpto(customlistview.getsize) get not the last index.
Also the scrolling finished not at last index. I suspect a problem with the itemhight.

How can I solve the problem?
Geritt
 

tunderin

Member
Licensed User
Longtime User
You would need to modify the customlistview class itself. The class uses a list called "items"

Try adding something like:

B4X:
Public Sub getNumItems As Int
    Return items.Size

End Sub
 

klaus

Expert
Licensed User
Longtime User
customlistview.jumpto(customlistview.getsize) get not the last index.
That sounds strange to me!
Add Log(customlistview.getsize) before the line customlistview.jumpto ... to check if the size is really wrong.
The JumpToItem routine adds the heights of all internal Panels to get the scroll value.

You should post your project or better a smaller one as a zip file showing the problem, so we can see what exactly you have done and how.
 

tunderin

Member
Licensed User
Longtime User
That sounds strange to me!

It sounds strange to you Klaus because it is. I just looked up the customlistview class code and what I described turns out to be the code for customlistview.size

Ignore me, I'm going for coffee...
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hallo,
i'm use the CustomListview with many items from a database.

customlistview.jumpto(customlistview.getsize) get not the last index.
Also the scrolling finished not at last index. I suspect a problem with the itemhight.

How can I solve the problem?
Geritt
I suspect the customlistview is larger than the screen and so the last item(s) are actually out of view?
 

Geritt Schlag

Member
Licensed User
Longtime User
Yes, they are out of view.

In LOG getsize value is correct.
But item_click to the last visible and scrolling item receive an index lower then getsize-value.
 
Last edited:

Geritt Schlag

Member
Licensed User
Longtime User
Yes, they are out of view.

In LOG getsize value is correct.
But item_click to the last visible and scrolling item receive an index lower then getsize-value.

I have the solution to the puzzle: the cause, was the wrong calculation of the amount of the visible area of the list view. If the height is defined correctly, then it's work. Thanks for your thoughts-impulses!!!

Geritt
 
Status
Not open for further replies.
Top