Android Code Snippet ShuffleList

Subname: ShuffleList

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
shufflelist001.png


After click on Shuffle Playlist
shufflelist002.png


Tags: shuffle playlist
 

Attachments

  • playlistshuffle.zip
    7.5 KB · Views: 550

William Lancee

Well-Known Member
Licensed User
Longtime User
Using B4XPages for B4J. For both use conditional compilation #if B4J #else if B4A #end if


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI

    Private playlist As List
    Private lv As ListView
    Private ButtonShuffle As Button
    Private ButtonPlay As Button
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.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.Items.Clear                    'CHANGE
    
    Log("===== PlayList (create) =========")
    For i = 0 To playlist.Size-1
        lv.Items.Add(playlist.Get(i))    'CHANGE
        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
    xui.MsgboxAsync("Next is " & GetNextFromPL, True)    'CHANGE
    lv.Items.Clear    'CHANGE
    For i = 0 To playlist.Size-1
        lv.Items.add(playlist.Get(i))        'CHANGE
        Log(playlist.Get(i))
    Next
    
End Sub

Sub ButtonShuffle_Click
    playlist = ShuffleList(playlist)
    Log("===== PlayList (shuffle) =========")
    lv.Items.Clear    'CHANGE
    For i = 0 To playlist.Size-1
        lv.Items.add(playlist.Get(i))        'CHANGE
        Log(playlist.Get(i))
    Next
    Log("=================================")
        
End Sub

Private Sub lv_SelectedIndexChanged(Index As Int)        'CHANGE
    
End Sub
 

Colin Evans

Active Member
Licensed User
Longtime User
Using B4XPages for B4J. For both use conditional compilation #if B4J #else if B4A #end if


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI

    Private playlist As List
    Private lv As ListView
    Private ButtonShuffle As Button
    Private ButtonPlay As Button
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.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.Items.Clear                    'CHANGE
   
    Log("===== PlayList (create) =========")
    For i = 0 To playlist.Size-1
        lv.Items.Add(playlist.Get(i))    'CHANGE
        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
    xui.MsgboxAsync("Next is " & GetNextFromPL, True)    'CHANGE
    lv.Items.Clear    'CHANGE
    For i = 0 To playlist.Size-1
        lv.Items.add(playlist.Get(i))        'CHANGE
        Log(playlist.Get(i))
    Next
   
End Sub

Sub ButtonShuffle_Click
    playlist = ShuffleList(playlist)
    Log("===== PlayList (shuffle) =========")
    lv.Items.Clear    'CHANGE
    For i = 0 To playlist.Size-1
        lv.Items.add(playlist.Get(i))        'CHANGE
        Log(playlist.Get(i))
    Next
    Log("=================================")
       
End Sub

Private Sub lv_SelectedIndexChanged(Index As Int)        'CHANGE
   
End Sub

Thanks for the input but I tried copy and paste into a new B4J project and got multiple errors, don't suppose you could attach a small B4J program so I could understand better, many thanks
 

Colin Evans

Active Member
Licensed User
Longtime User
thank you. Just implemented it into my Media player and it works a dream, thanks again.
Attached is my noddy version of an MP3 player using playlists held in .txt files, please excuse my rough coding
 

Attachments

  • DITL MP3 Player.zip
    3.6 KB · Views: 213
Last edited:
Top