Android Example Shuffle a LIST (MP3 playlist for example)

In this example you can find a short example which shuffles a playlist (mp3´s for ex.).
Here i´m using the alphabet (A to Z)

Code:
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


The mainpart of this example are the following two subs

Code:
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

This sub SHUFFLES a LIST. It needs a list given as parameter and it returns a new version of the given list. In example it is used like

Code:
playlist = ShuffleList(playlist)

And the second sub gets the first item of this list. and the global list is changed
get first item of list and remove it from list
put the removed item to end of list
the sub returns the item to the caller

Code:
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

I dont know whether it´s useful in this example but i think the principle of ramdonly shuffles LISTs could be interesting to some of you. So i decided to create this example-thread here :-)

Feel free to add your code-suggestions to this and reshare your knowledge :-)
 

Attachments

so thanks
 
Brilliant, wouldn't expect anything less from DonManfred, just what I needed for my MP3 player
 
Back
Top