java - Make app more effecient by eliminating extra codes -


i have following code starts a way z :

if (someid.matches("a") || someid.matches("a")) {     tvletcap.settext("a");     tvletlow.settext("a");     ivlettericon.setimageresource(r.drawable.apple);     btndisplayword.settext("a apple");     btnplay.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {             stopplaying();             mpsound = mediaplayer.create(myclass.this, r.raw.a);             mpsound.setlooping(false);             mpsound.start();             btnplay.setvisibility(view.gone);             btnstop.setvisibility(view.visible);             btnstop.setonclicklistener(stopsound);             mpsound.setoncompletionlistener(new oncompletionlistener() {                 @override                 public void oncompletion(mediaplayer mp) {                     btnplay.setvisibility(view.visible);                     btnstop.setvisibility(view.gone);                 }             });         }     }); }        if (someid.matches("b") || someid.matches("b")) {     tvletcap.settext("b");     tvletlow.settext("b");     ivlettericon.setimageresource(r.drawable.ball);     btndisplayword.settext("b ball");     btnplay.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {                 stopplaying();                 mpsound = mediaplayer.create(myclass.this, r.raw.b);             mpsound.setlooping(false);                 mpsound.start();             btnplay.setvisibility(view.gone);             btnstop.setvisibility(view.visible);             btnstop.setonclicklistener(stopsound);             mpsound.setoncompletionlistener(new oncompletionlistener() {                 @override                 public void oncompletion(mediaplayer mp) {                     btnplay.setvisibility(view.visible);                     btnstop.setvisibility(view.gone);                 }             });         }     }); } 

would work oppose above:

switch(someid) {     case a:         setletter("a");         addicon("apple");         break;     case b:         setletter("b");         addicon("ball");         break;     default:         break; }  public void setletter(string strletter) {     tvletcap.settext(strletter);     tvletlow.settext(strletter.tolowercase()); } public void addicon(string iconletter) {     ivlettericon.setimageresource(r.drawable. + iconletter);     btndisplayword.settext(iconletter.charat(0).touppercase() + " " + iconletter.touppercase()); } 

i guessing issue might line , how fix it?:

ivlettericon.setimageresource(r.drawable. + iconletter); 

also possible take entire btnplay function different function , pass letter did other functions it's not repeated over?

simplest solution: add parameter addicon:

public void addicon(string iconletter, int iconresource){     ivlettericon.setimageresource(iconresource);     btndisplayword.settext(iconletter.charat(0).touppercase() + " " + iconletter.touppercase()); } 

then call with

seticon("apple", r.drawable.apple); 

as far reusing on click listener, it's possible. have single onclick listener that's listening views. when view clicked, it's passed in argument onclick(view view) method. can use find start/stop button view , whatever them. also, there's useful (and abused) method pair: settag(object object), , gettag(). might want using store resource id raw audio file want play when button clicked.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -