Results 1 to 5 of 5
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default convert string content to a variable name

    Java Code:
    int counter = 0;
    
    public void onClick(View view)
    	{
    
    		Log.i("onClick", "BEGIN");
    		String imageNumber= "img";
    		
    		counter++;
    		imageNumber+= counter;
    			
    		ImageView image = (ImageView) findViewById(R.id.imageView1);
    
    		image.setImageResource(raw.[B]imageNumber[/B]);
    		Log.i("onClick", "END");
    	}
    I am attempting to write a program that when you click the button the next image will come up in the display. The images names that I want to display are img1, img2 etc.

    the problem with the code I have is in the line
    Java Code:
    image.setImageResource(raw.imageNumber);
    that line of code sets the image to the picture it is supposed to be. The problem is that instead of imageNumber I need it to be the content of the image number. So I need to convert the content of the string to a variable name.

    Thanks!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default Re: convert string content to a variable name

    The short answer is that you can't do that. What you want is some sort of collection, or at least a simple Array.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: convert string content to a variable name

    You don't want to do this anyway. I agree with gcalvin -- use a HashMap or array.

  4. #4
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default Re: convert string content to a variable name

    ok so I would want to just have an array with all of the variables and go to the next element in the array each time the button is clicked?

    so something like this
    Java Code:
    int counter = 0;
    int[] picArray = new int[10];
    picArray[0] = r.id.img1
    picArray[1] = r.id.img1
    // etc 
     
    public void onClick(View view)
        {
     
            Log.i("onClick", "BEGIN");
            counter++;
                 
            ImageView image = (ImageView) findViewById(R.id.imageView1);
     
            image.setImageResource(raw.picArray[counter]);
            Log.i("onClick", "END");
        }
    Last edited by hoosierfan24; 10-13-2011 at 03:33 PM.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default Re: convert string content to a variable name

    I'm saying that instead of discrete variables img1 and img2, you want an array, so that you have img[0] and img[1]. Then it's easy to choose one or the other by specifying the index.

Similar Threads

  1. Replies: 1
    Last Post: 04-04-2011, 01:51 PM
  2. Replies: 1
    Last Post: 02-17-2011, 06:21 PM
  3. Convert variable name to string.
    By MHardeman25 in forum New To Java
    Replies: 9
    Last Post: 08-17-2010, 10:18 PM
  4. format content of String[]
    By eponcedeleon in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2010, 06:12 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •