i have hundreds of buttons, same listener , action, different names. here 1 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 have 1 method name of button passed method parameter. this, 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
, r.id.mot
don't this! should have hundreds of buttons, or there way around problem? thanks
you looking resources.getidentifier
. looks resource id string name , 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); ...
Comments
Post a Comment