Android Question Unable to clear a listview

Serge Bertet

Active Member
Licensed User
Longtime User
hi,

do I need to loop over all items to clear an whole listview?
it looks like only the selected item is cleared with the clear method.
With
Code: Copy
PlayListView.Clear
or
Code: Copy
    Do While PlayListView.Size > 0
        PlayListView.RemoveAt(0)
    Loop
I always have new text over old text in some cells.
So looking for a way clear and refresh (refill) a listview with new values.

Thx
 
Clear should remove all items from the list. Can you post all the related code?

- Colin.
 
Upvote 0
Here is the code used to refresh the listview:
Code: Copy
Sub ShowList
    PlayListView.Clear
    csr = Starter.db.ExecQuery("SELECT * FROM 'playlists' ORDER BY `playlistname` ASC")
    If csr.RowCount > 0 Then
        For i = 0 To csr.RowCount-1
            csr.Position = i
            PlayListView.AddSingleLine2(csr.GetString("playlistname"), csr.GetInt("ID"))
        Next
    End If
    ActionPanel.visible = False
End Sub
Hope that helps to help me.

Serge
 
Upvote 0
Here is the code used to refresh the listview:
Code: Copy
Sub ShowList
    PlayListView.Clear
    csr = Starter.db.ExecQuery("SELECT * FROM 'playlists' ORDER BY `playlistname` ASC")
    If csr.RowCount > 0 Then
        For i = 0 To csr.RowCount-1
            csr.Position = i
            PlayListView.AddSingleLine2(csr.GetString("playlistname"), csr.GetInt("ID"))
        Next
    End If
    ActionPanel.visible = False
End Sub
Hope that helps to help me.

Serge
So what happens when you run this code? The if statement is redundant btw. If there are no records in the cursor, the for loop won't execute.

- Colin
 
Upvote 0
Yes, you'r rigth, the If is not needed here, but it is waiting for an Else ....:)
When I execute this code I can see the old text overwritted with the new one and at the end both (old ans new) are visible.
When I run step by step using the debugger it looks like only le last selected item is erased.
Android version on my test tablet is 4.2.1
Also used:
Code: Copy
    Do While PlayListView.Size > 0
        PlayListView.RemoveAt(0)
    Loop
with no result.

Serge
 
Last edited:
Upvote 0
As per LucaMs advice I switched to xCutomListView but there is stil a ListView in another activity.
Yes I never close the cursor, maybe the problem is here, thx Mahares, i'll try.
.. and try with a delay ... I can see my tablet slowing down debug after debug until I restart.
For now I'm stuck with syntaxes containing $ I don't know what it is used for .... even "Beginners Guide" and "B4x Langage Basic" don't talk about that ... but this is matter for another thread.
Thx for all
Serge
 
Upvote 0
Upvote 0
In fact it is in an example for CustomListView (CustomListView.b4a) found on this web site
Code: Copy
    For i = 1 To 3
        clv2.Add(CreateListItem($"Item #${i}"$, clv2.AsView.Width, 70dip), 70dip, $"Item #${i}"$)
    Next
I'll check your link.
 
Upvote 0
It is always difficult to help with small code snippets.
It is always much more efficient to post a small project showing the problem, or even post the whole project (at least for me).
Most of the time, the problem is not in the code snippet but somewhere else in the code.

In your last post you refer to CustomListView, but you should use xCustomListView.
 
Upvote 0
Back
Top