2012-09-03 5 views
5

ho questo codice:Android: passare un ID di risorsa come parametro

translateRight("iv1"); //iv1 is an id of an imageView 

    private void translateRight(String st){ 

    ImageView img = (ImageView)findViewById(R.id.st); 
     Animation a = AnimationUtils.loadAnimation(this, R.anim.translate_right); 
     img.startAnimation(a); 
    } 

ho "translateRight", che è un metodo che mostrano un'animazione, ma a questo metodo che ho dovuto passare un ID di risorsa; Ho provato con una stringa ma non funziona, cosa posso fare?

+0

Tra l'altro - è più comoda per passare 'ImageView' stessa. –

risposta

9

ID risorsa: è un valore intero. Basta passare un int:

private void translateRight(int resource){ 
    ImageView img = (ImageView) findViewById(resource); 
    //stuff 
} 
5

ID risorsa è un numero intero, non una stringa.