Loading resources with a variable


Last Updated:

  1. Woulou

    Woulou Member This Topic's Starter

    Joined:
    Jul 11, 2010
    Messages:
    6
    Likes Received:
    0
    Hello,
    I have a problem which is the following : I can't load a resources from a variable (which would contain the id or the name of the resource):
    For instance if you do :
    Code (Text):
    1.  
    2. Button button = (button)findViewById (R.id.buttonNb12)
    3.  
    (With an XML containing a button with id "buttonNb12" of course ;))
    It will work.

    But if you have your Id in a String how can you do it ? The following method doesn't work :( :
    Code (Text):
    1.  
    2. String x = "buttonNb12"
    3. Button button = (button)findViewById (R.id.(x))
    4.  
    or this ...

    Code (Text):
    1.  
    2. String x = "R.id.buttonNb12"
    3. Button button = (button)findViewById(x))
    4.  
    Do you have a solution please ?
    Thanks
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    There are a few different solutions. The best one will depend on how flexible you want it to be, and how many different IDs you want to deal with.

    The most obvious solution: just compare strings, and assign the correct ID value to an int variable.

    Code (Text):
    1.  
    2.       private int getIdFromString( String idStr )
    3.       {
    4.           int id = 0 ;
    5.  
    6.           if( idStr.equals("R.id.button1") )
    7.           {
    8.               id = R.id.button1 ;
    9.           }
    10.           else
    11.           if( idStr.equals("R.id.button2") )
    12.           {
    13.               id = R.id.button2 ;
    14.           }
    15.           else
    16.           . . . etc . . .
    17.  
    18.           return id ;
    19.       }
    20.  
    21.       private void something()
    22.       {
    23.           String x = "R.id.buttonNb12" ;
    24.  
    25.           int id = getIdFromString(x) ;
    26.           Button button = (button)findViewById(id) ;
    27.        
    28.           . . . etc . . .
    29.       }
    30.  
    31.  

    If you have lots of IDs then it might be cumbersome to have a very long set of if statements.

    You could set up all the IDs in a Map, and then use the string to look up the ID in the Map.

    Code (Text):
    1.  
    2.       private Map<String, Integer> m_idMap = new HashMap<String, Integer>() ;
    3.  
    4.       private void setUpIDs()
    5.       {
    6.           m_idMap.put( "R.id.button1", new Integer(R.id.button1) ) ;
    7.           m_idMap.put( "R.id.button2", new Integer(R.id.button2) ) ;
    8.           m_idMap.put( "R.id.button3", new Integer(R.id.button3) ) ;
    9.           . . . etc . . .
    10.          
    11.       }
    12.  
    13.       private void something()
    14.       {
    15.           String x = "R.id.buttonNb12" ;
    16.  
    17.           int id = m_idMap.get(x).intValue() ;
    18.           Button button = (button)findViewById(id) ;
    19.        
    20.           . . . etc . . .
    21.       }
    22.  
    The code does just enough to illustrate the solutions. Obviously you'll to put additional error checking in.

    There's one other solution that would involve using Java Reflection. It's a bit more complicated. I can describe it if you need it, if the other solutions won't work for you.

    Mark
     
    JiMMaR and Woulou like this.
  3. Woulou

    Woulou Member This Topic's Starter

    Joined:
    Jul 11, 2010
    Messages:
    6
    Likes Received:
    0
    Thanks so much, your answer is very clear. In fact I realize that the key I was looking for is getIdFromString () but I'll have also to use map later in my code so your answer perfectly suit my needs :D.
    In fact what seemed strange to me is that the Id is not a string (although it can contain letters) .

    Thanks again !
     
  4. Woulou

    Woulou Member This Topic's Starter

    Joined:
    Jul 11, 2010
    Messages:
    6
    Likes Received:
    0
    Sorry to ask something again but if I have 50 buttons, can I make a loop creating the map ?
    can I do :

    m_idMap.put( "R.id.button", new Integer(R.id.button) ) ;

    Where I make i goes from 1 to 50 ?
     
  5. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    I'm afraid not. You can't do that (well not easily anyway)

    Code (Text):
    1.  
    2. m_idMap.put( "R.id.button[i]", new Integer(R.id.button[i]) ) ;
    3.  
    That takes you back to your original problem of turning an unknown ID into an integer.

    You can't do

    Code (Text):
    1.  
    2.  R.id.button[i]
    3.  
    because button isn't an array.

    If you're just looking to set up 50 buttons, and you know the IDs in advance, then it might just be easier to do lots of typing, or copy/paste.
    (Won't your GUI be a little cramped with 50 buttons on the screen?)

    If you want a fully dynamic solution where you can turn the string for any ID into an int, then that's possible using Java reflection. It might be overkill if you just want to save some typing. But if you really need it, I can describe a potential way of doing it. I'll have to go away and write the code first.
     
  6. Woulou

    Woulou Member This Topic's Starter

    Joined:
    Jul 11, 2010
    Messages:
    6
    Likes Received:
    0
    Ok then, if you don't mind I'd like to know more about this solution (I'm giving the example of 50 buttons but my case needs indeed reflection).
    Thanks
     
  7. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Here's some code that will set up a Map with all the ids under R.id.*

    Code (Text):
    1.  
    2.     private Map<String, Integer> m_idMap = new HashMap<String, Integer>() ;
    3.    
    4.     private void createIDMap() {
    5.         R.id ids = new R.id() ;
    6.        
    7.         // Get all the fields declared under R.id
    8.         Class c = R.id.class ;
    9.         Field fields[] = c.getDeclaredFields() ;
    10.        
    11.         try {
    12.             int i = 0 ;
    13.             while( i<fields.length ) {
    14.                 Field field = fields[i] ;
    15.                 String idStr = "R.id." + field.getName() ;
    16.                 int    idVal = field.getInt(ids) ;
    17.                
    18.                 m_idMap.put( idStr, new Integer(idVal) ) ;
    19.                 Log.d(TAG, "Adding "+idStr+" with value "+idVal) ;
    20.                 i++ ;
    21.             }
    22.         } catch (IllegalArgumentException e) {
    23.             // TODO Auto-generated catch block
    24.             e.printStackTrace();
    25.         } catch (IllegalAccessException e) {
    26.             // TODO Auto-generated catch block
    27.             e.printStackTrace();
    28.         }
    29.     }
    30.  
    Here's the debug output that I get:

    Code (Text):
    1.  
    2. 07-12 00:07:45.087: DEBUG/TempestActivity(1568): Adding R.id.intro with value 2131099657
    3. 07-12 00:07:45.092: DEBUG/TempestActivity(1568): Adding R.id.level with value 2131099651
    4. 07-12 00:07:45.092: DEBUG/TempestActivity(1568): Adding R.id.life with value 2131099650
    5. 07-12 00:07:45.092: DEBUG/TempestActivity(1568): Adding R.id.logo with value 2131099652
    6. 07-12 00:07:45.102: DEBUG/TempestActivity(1568): Adding R.id.scores with value 2131099654
    7. 07-12 00:07:45.102: DEBUG/TempestActivity(1568): Adding R.id.screenshot with value 2131099653
    8. 07-12 00:07:45.112: DEBUG/TempestActivity(1568): Adding R.id.start with value 2131099655
    9. 07-12 00:07:45.112: DEBUG/TempestActivity(1568): Adding R.id.status with value 2131099648
    10. 07-12 00:07:45.112: DEBUG/TempestActivity(1568): Adding R.id.title with value 2131099656
    11.  
    You can then look up any id given a string, as follows

    Code (Text):
    1.  
    2.   int id = m_idMap.get("R.id.start").intValue() ;
    3.  
    So to loop over 50 buttons, button1 to button50, you could write this:

    Code (Text):
    1.  
    2.   for( int i=0; i<50; i++ )
    3.   {
    4.     String idStr = "R.id.button" + (i+1) ;
    5.     int id = m_idMap.get(idStr).intValue() ;
    6.     . . . etc . . .
    7.   }
    8.  
    9.  
     
    Woulou likes this.
  8. Woulou

    Woulou Member This Topic's Starter

    Joined:
    Jul 11, 2010
    Messages:
    6
    Likes Received:
    0
    Thanks so much, it works :)!
     

Share This Page

Loading...