Android Question Using variable as image name

Discussion in 'Android Questions' started by Reproretro, Jul 2, 2015.

  1. Reproretro

    Reproretro Member Licensed User

    Apologies if this has been asked before.

    I have about 100 images: icon_000, icon_001 to icon_100

    A number will be entered and the appropriate icon displayed. Is there any way to Dim, Initialize and use them as a variable, rather than repeating everything 100 times?
     
  2. sorex

    sorex Expert Licensed User

    you can build your name with

    name="icon_"&x

    where x can be edtNumer.text or a loop counter
     
  3. sorex

    sorex Expert Licensed User

    maybe it was wrong advice as I thought icon_000 was the filename of the icon.

    It all depends on how the app will work, it if just shows 1 icon after a number is entered than the hint is valid
    and you can use loadbitmap with 1 imageview and the variable filename instead of 100 imageviews that you hide and just show 1.
     
  4. Reproretro

    Reproretro Member Licensed User

    Sorry, my bad
    They are icon_000.gif, icon_002.gif ... icon_100.gif

    And I want to avoid

    Dim icon000, icon001, ... icon100 as bitmap

    icon_000.Initialize(File.DirAssets,"icon_000.gif")
    icon_000.Initialize(File.DirAssets,"icon_001.gif")
    ...
    icon_000.Initialize(File.DirAssets,"icon_100.gif")


    Select x
    case x = 0
    imgIcon.bitmap = icon_000
    case x = 1
    imgIcon.bitmap = icon_001
    ...
    case x = 100
    imgIcon.bitmap = icon_100
    end select
     
  5. sorex

    sorex Expert Licensed User

    Code:
    Dim icon as imageview
    dim nt as string
    icon.initialize(
    "")

    nt=
    1
    nt=
    "00"&nt
    nt=nt.SubString(nt.Length-
    3)
    icon.bitmap=
    loadbitmap(file.dirassets,"icon_"&nt&".gif")
    changing nt to 1, 64 or 397 will always remain in the 3 digit format.
     
    Reproretro and JordiCP like this.
  6. Reproretro

    Reproretro Member Licensed User

    I tried that but it produces a compile error - src\b4a\example\main.java:956: error: incompatible types: String cannot be converted to Bitmap
     
  7. sorex

    sorex Expert Licensed User

    just tried it and it works fine here.
     
  8. Reproretro

    Reproretro Member Licensed User

    Thank you for that. I will work on it tomorrow, it's past my bedtime.
    But thanks for confirming it works, so I know I have to look elsewhere my code for the problem
     
  9. Reproretro

    Reproretro Member Licensed User

    It's working now.
    Thank you :)
     
    DonManfred likes this.
  10. sorex

    sorex Expert Licensed User

    you're welcome
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice