Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:23:59+00:00 2026-06-14T09:23:59+00:00

I was trying to save time by using a for loop to initialize all

  • 0

I was trying to save time by using a for loop to initialize all the sprites i wanted on screen. I had a couple of images on my drawable folder. So I renamed them accordingly. I wanted to concatenate the i to the expression, then it had to be called/invoked somehow!
At first I thought about doing this:

Integer.parseInt("R.drawable.scoredisplay" + i)

Then, of course you cant parse that into an integer, also the result of that expression returns an integer anyway. How can I make the concatenation of the expression with i and then call it? is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:24:00+00:00Added an answer on June 14, 2026 at 9:24 am

    It’s possible, you need to do the following:

    Context x = getApplicationContext();
    int myId  = x.getResources().getIdentifier("R.drawable.scoredisplay" + i, "drawable",x.getPackageName());
    

    Where the first parameter is the id as String of the View you want to get, the second one is the type of resource in this case “drawable”, and the third one the package name which we obtain also from the context, calling getPackageName() method.
    Then afterwards you can get the view with the following code:

    View myView = findViewById(myId);
    

    Casting the string to an integer as you were thinking makes no sense, since the id is not a string (although it has a workaround like I’m showing you).

    Update

    Since you are using this code outside of an Activity class the calls to the Context methods are not valid. You need to create a way of accessing the Activity Context from outside of the activity (you mention calling .getContext() from your class, but that will get that classes context, not the activity). A way of achieving this would be modifying your constructor, lets say you have a class named myClass:

    class myClass{
        //Declase a Context variable inside your class
        Context x;
    
        //You implement a constructor for this class that accepts a Context as 
        //a parameter (feel free to add more if you are using a constructor already)
        public myClass(Context applicationContext){
            //Assign the passed value to your local Context
            x = applicationContext;
        }
    
        //Afterwards, on a different part of your class, you could invoke activity 
        //related methods by using the Context you have 'x'
        public void otherMethod(){
           int myId  = x.getResources().getIdentifier("R.drawable.scoredisplay" + i, "drawable",x.getPackageName());
        }
    }
    

    The last part in ensuring you are passing your value correctly from your Activity, you should see something similar to this somewhere in your code:

    myClass i = new myClass();
    

    Since we now have a constructor, or have modified the existing one, you can add this to pass the activity context right into your game or whatever class your creating:

    myClass i = new myClass(this);//'this' can be 'getApplicationContext()'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to save myself some coding time by taking advantage of using namespace
I'm trying to save a CUploadedFile object in local folder, I am using img
Im trying to record video using AVFoundation I can save images but not video.
i am trying to save (using linq) the date and time to an SQL
I am trying to save a couple of web pages by using a web
Recently I've been trying to save on render time by minimizing the amount of
I'm trying to build a little renaming program to help save me time in
I'm having hard time trying to figure out how to auto-save user data in
im trying to save a row in my settings table, it works all fine
When trying to save the database using boost serialization, I encounter the segfault that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.