How to populate Dir.Assets files

Scantech

Well-Known Member
Licensed User
Longtime User
How to get a list of .ttf files from Dir.Assets? I get errors with this.

B4X:
      Dim lstFont As List
      lstFont.Initialize
'      lstFont.Add("Digital.ttf"): lstFont.Add("Wlm.ttf"): lstFont.Add("LcdPhone.ttf")
      
      lstFont.AddAll(File.ListFiles(File.DirAssets))
      For x = 0 To lstFont.Size -1
         y = lstFont.Get(x)
         If y.EndsWith(".ttf") = False Then 
            lstFont.RemoveAt(x)
         End If
      Next
      lstFont.Sort(True)
 

klaus

Expert
Licensed User
Longtime User
Change the line below:
B4X:
lstFont = File.ListFiles(File.DirAssets)
File.ListFiles returns a list!

I would also suggest you to modify the For / Next loop because when you remove one item the lstFont.Size changes:
B4X:
For x = lstFont.Size -1 To 0 Step -1
    y = lstFont.Get(x)
    If y.EndsWith(".ttf") = False Then 
        lstFont.RemoveAt(x)
    End If
Next
Best regards.
 
Last edited:
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Change the line below:
B4X:
lstFont = File.ListFiles(File.DirAssets)
File.ListFiles returns a list!

I would also suggest you to modify the For / Next loop because when you remove one item the lstFont.Size changes:
B4X:
For x = lstFont.Size -1 To 0 Step -1
    y = lstFont.Get(x)
    If y.EndsWith(".ttf") = False Then 
        lstFont.RemoveAt(x)
    End If
Next
Best regards.


Thank you Klaus. However, error with
B4X:
lstFont = File.ListFiles(File.DirAssets)

I just kept it the way it was
B4X:
lstFont.AddAll(File.ListFiles(File.DirAssets))
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
What error do you get ?
I tested lstFont = File.ListFiles(File.DirAssets) before I posted it to be sure that it works OK.
So I'm estonished that you get an error.

Best regards.

LastException java.lang.UnsupportedOperationException

lstFont.RemoveAt(x) sets the error using lstFont = File.ListFiles(File.DirAssets)

But lstFont.AddAll(File.ListFiles(File.DirAssets)) does not trigger this error.

lstFont size = 55 and error at x = 54???
 
Last edited:
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:645)
at anywheresoftware.b4a.objects.collections.List.RemoveAt(List.java:94)
at Scantech.CarGaugePro.main._createpreferencescreen(main.java:2723)
at Scantech.CarGaugePro.main._activity_create(main.java:416)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at Scantech.CarGaugePro.main.afterFirstLayout(main.java:84)
at Scantech.CarGaugePro.main.access$100(main.java:16)
at Scantech.CarGaugePro.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
java.lang.UnsupportedOperationException
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tried as you with lstFont = File.ListFiles(File.DirAssets) and lstFont.RemoveAt (x) raises an error as you reported independant what value x has.
I think I understand why:
With lstFont = File.ListFiles(File.DirAssets) lstFont points to theFile.ListFiles(File.DirAssets) List and this one can't be modified.
With lstFont.AddAll(File.ListFiles(File.DirAssets)) lstFont is a new List instance with all possibilities.

I'm happy, I already learned something today.

Best regards.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I tried as you with lstFont = File.ListFiles(File.DirAssets) and lstFont.RemoveAt (x) raises an error as you reported independant what value x has.
I think I understand why:
With lstFont = File.ListFiles(File.DirAssets) lstFont points to theFile.ListFiles(File.DirAssets) List and this one can't be modified.
With lstFont.AddAll(File.ListFiles(File.DirAssets)) lstFont is a new List instance with all possibilities.

I'm happy, I already learned something today.

Best regards.

That is a good explanation. Thank you for your time :)
 
Upvote 0
Top