Subname: ShuffleList
Description: Shuffles a list. Can be used for example to shuffle a Mp3-Playlist
Example
After start
After click on Shuffle Playlist
Tags: shuffle playlist
Description: Shuffles a list. Can be used for example to shuffle a Mp3-Playlist
B4X:
Sub ShuffleList(pl As List) As List
For i = pl.Size - 1 To 0 Step -1
Dim j As Int
Dim k As Object
j = Rnd(0, i + 1)
k = pl.Get(j)
pl.Set(j,pl.Get(i))
pl.Set(i,k)
Next
Return pl
End Sub
Example
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim playlist As List
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private lv As ListView
Private ButtonShuffle As Button
Private ButtonPlay As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
playlist.Initialize
playlist.Add("A")
playlist.Add("B")
playlist.Add("C")
playlist.Add("D")
playlist.Add("E")
playlist.Add("F")
playlist.Add("G")
playlist.Add("H")
playlist.Add("I")
playlist.Add("J")
playlist.Add("K")
playlist.Add("L")
playlist.Add("M")
playlist.Add("N")
playlist.Add("O")
playlist.Add("P")
playlist.Add("Q")
playlist.Add("R")
playlist.Add("S")
playlist.Add("T")
playlist.Add("U")
playlist.Add("V")
playlist.Add("W")
playlist.Add("X")
playlist.Add("Y")
playlist.Add("Z")
lv.Clear
Log("===== PlayList (create) =========")
For i = 0 To playlist.Size-1
lv.AddSingleLine(playlist.Get(i))
Log(playlist.Get(i))
Next
Log("=================================")
End Sub
Sub ShuffleList(pl As List) As List
For i = pl.Size - 1 To 0 Step -1
Dim j As Int
Dim k As Object
j = Rnd(0, i + 1)
k = pl.Get(j)
pl.Set(j,pl.Get(i))
pl.Set(i,k)
Next
Return pl
End Sub
Sub GetNextFromPL As String
Dim t As Object
Dim res As String
t = playlist.Get(0)
res = t
playlist.RemoveAt(0)
playlist.Add(t)
Return res
End Sub
Sub ButtonPlay_Click
ToastMessageShow("Next is "&GetNextFromPL,True)
lv.Clear
For i = 0 To playlist.Size-1
lv.AddSingleLine(playlist.Get(i))
Log(playlist.Get(i))
Next
End Sub
Sub lv_ItemClick (Position As Int, Value As Object)
End Sub
Sub ButtonShuffle_Click
playlist = ShuffleList(playlist)
Log("===== PlayList (shuffle) =========")
lv.Clear
For i = 0 To playlist.Size-1
lv.AddSingleLine(playlist.Get(i))
Log(playlist.Get(i))
Next
Log("=================================")
End Sub
After start
After click on Shuffle Playlist
Tags: shuffle playlist