Andy
Andy

Reputation: 99

android studio find by id, where id is a variable passed as a parameter

I have hundreds of buttons, all with the same listener and action, but different names. Here is one with the name goes1:

public void goes1(){
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.goes1);
    play_text_view = (TextView) this.findViewById(R.id.goes1);
    play_text_view.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            try {
                lireXML("goes1");
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mp.start();
        }
    });
}

I would like to have only one method where the name of the button would be passed to the method as a parameter. Something like this, with the String parameter mot:

public void playMot(String mot){
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.mot);
    play_text_view = (TextView) this.findViewById(R.id.mot);
    play_text_view.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            try {
                lireXML(mot);
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mp.start();
        }
    });
}

but R.raw.mot and R.id.mot don't like this! Should I really have hundreds of buttons, or is there a way around my problem? Thanks

Upvotes: 0

Views: 5076

Answers (2)

iagreen
iagreen

Reputation: 32026

You are looking for Resources.getIdentifier. It looks up a resource id from a string name and type.

public void playMot(String mot) {
    Resources res = getResources();
    // Should error check this, returns 0 on error.
    final rawId = res.getIdentifier(mot, "raw", getPackageName());  
    final MediaPlayer mp = MediaPlayer.create(this, rawId);
    // Should error check this, returns 0 on error.
    final resId = res.getIdentifier(mot, "id", getPackageName());  
    play_text_view = (TextView) this.findViewById(resId);
    ...

Upvotes: 1

simonws
simonws

Reputation: 65

You can story the relation between name and raw id,the relation between name and view id,in hashMap,like this:

        HashMap<String, Long> nameToRawId = new HashMap<String, Long>();
        HashMap<String, Long> nameToViewId = new HashMap<String, Long>();

then get id from that hashMap int method playMot(String mot):

public void playMot(String mot){
final MediaPlayer mp = MediaPlayer.create(this, nameToRawId.get(mot));
play_text_view = (TextView) this.findViewById(nameToViewId.get(mot));
play_text_view.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
        try {
            lireXML(mot);
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
    }
});
}

hope this can help you.

Upvotes: 0

Related Questions