loops - Android Studio: Iterate through images in drawable for onClick imageButton -


i have 8 imagebuttons set id's imagebutton1 imagebutton8.

when click on particular button (imagebutton2 example) want loop through images in drawable folder named image1....image10, , set image imagebutton each click.

i have looked @ various loops solve there have led nowhere. here snippet of loop attempt:
updated 26/4/16
solved

//updated 25/4/16 imagebutton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8; imagebutton[] btns; int[] drawables = new int[]{r.drawable.image1,r.drawable.image2,r.drawable.image3,r.drawable.image4,r.drawable.image5,r.drawable.image6,r.drawable.image7,r.drawable.image8};   @override protected void oncreate(bundle savedinstancestate) {        btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);     btn2 = (imagebutton) findviewbyid(r.id.imagebutton2);     btn3 = (imagebutton) findviewbyid(r.id.imagebutton3);     btn4 = (imagebutton) findviewbyid(r.id.imagebutton4);     btn5 = (imagebutton) findviewbyid(r.id.imagebutton5);     btn6 = (imagebutton) findviewbyid(r.id.imagebutton6);     btn7 = (imagebutton) findviewbyid(r.id.imagebutton7);     btn8 = (imagebutton) findviewbyid(r.id.imagebutton8);          btn1.setonclicklistener(this);         btn2.setonclicklistener(this);         btn3.setonclicklistener(this);         btn4.setonclicklistener(this);         btn5.setonclicklistener(this);         btn6.setonclicklistener(this);         btn7.setonclicklistener(this);         btn8.setonclicklistener(this); }   btns = new imagebutton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8}; }  int counter = 0;         @override     public void onclick(view v)         {              counter ++;               (int = 0; < btns.length; i++)             {                   if (v.getid() == btns[i].getid())                 {                     if(counter ==8)                     {                         counter =0;                     }                          ((imagebutton)v).setimageresource(drawables[counter]);                  }              } } } 

updated 26/4/16 solved

another attempt create counter store number of clicks , set if statements change images according each click.

the problem here creates huge amount of code considering have multiple buttons.

on top of when run on 2 different buttons, once tap on imagebutton1 few times, imagebutton2 few times, imagebutton1 doesn't respond anymore.

int counter =0;          @override     public void onclick(view v)         {             counter++;               switch(v.getid())             {                 case r.id.imagebutton1:                     if(counter == 1){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image1);                         break;                      }else if (counter == 2){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image2);                         break;                      }else if (counter == 3){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image3);                          break;                      }else if (counter == 4){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image4);                          break;                     }else if (counter == 5){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image5);                         break;                      }else if (counter == 6){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image6);                          break;                      }else if (counter == 7){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image7);                          break;                     }else if (counter == 8){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image8);                         break;                      }else if (counter == 9){                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image9);                          break;                      }else if (counter == 10) {                          imagebutton btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);                         btn1.setimageresource(r.drawable.image10);                         counter = 0;                         break;                     }                     break; 

i appreciate guidance, meanwhile if solve issue repost edit. cheers

just suggestion.

maybe declare buttons , image resources arrays loop through them.

for example:

public class sample extends activity {    imagebutton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;   imagebutton[] btns;   int[] drawables = new int[]{r.drawable.imagebutton1,r.drawable.imagebutton2,r.drawable.imagebutton3,r.drawable.imagebutton4,r.drawable.imagebutton5,r.drawable.imagebutton6,r.drawable.imagebutton7,r.drawable.imagebutton8};   int[] origdrawables = new int[]{r.drawable.imageorigbutton1,r.drawable.imageorigbutton2,r.drawable.imageorigbutton3,r.drawable.imageorigbutton4,r.drawable.imageorigbutton5,r.drawable.imageorigbutton6,r.drawable.imageorigbutton7,r.drawable.imageorigbutton8};    @override   public void oncreate(bundle saveinstancestate) {     btn1 = (imagebutton) findviewbyid(r.id.imagebutton1);     btn2 = (imagebutton) findviewbyid(r.id.imagebutton2);     btn3 = (imagebutton) findviewbyid(r.id.imagebutton3);     btn4 = (imagebutton) findviewbyid(r.id.imagebutton4);     btn5 = (imagebutton) findviewbyid(r.id.imagebutton5);     btn6 = (imagebutton) findviewbyid(r.id.imagebutton6);     btn7 = (imagebutton) findviewbyid(r.id.imagebutton7);     btn8 = (imagebutton) findviewbyid(r.id.imagebutton8);      btn1.setonclicklistener(this);     btn2.setonclicklistener(this);     btn3.setonclicklistener(this);     btn4.setonclicklistener(this);     btn5.setonclicklistener(this);     btn6.setonclicklistener(this);     btn7.setonclicklistener(this);     btn8.setonclicklistener(this);      btns = new imagebutton[]{btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8};   }    @override   public void onclick(view v) {     (int = 0; < btns.length; i++) {       if (v.getid == btns[i].getid) {         ((imagebutton)v).setimageresource(drawables[i]);       } else {         ((imagebutton)v).setimageresource(origdrawables[i]);       }     }   }  } 

note: pardon code/syntax errors did using notepad.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -