android - Can the usage of "R.id" before View's name be avoided? -


i'm working on tictactoe app uses 9 imageviews. imageviews clickable , on click, either cross or circle image shown, followed game's logic. since resource file name needs updated every imageview, need write this:

@override public void onclick(view v) {  switch (v.getid()) {  case r.id.oneimage:     // code     break;  case r.id.twoimage:     // code     break;  case r.id.threeimage:     // code     break; //and on 6 more cases default:     break;     }  } 

while task need pretty same views, i looking way generic, if possible like:

v.getid().setimageresource(r.drawable.cross); 

or

int ivid= v.getid(); string name= "r.id." + ivid; integer.valueof(name).setimageresource(r.drawable.cross); 

both techniques can't resolve setimageresource() method , it's understandable why.

i figured none of these methods work , can't seem think of right query search on google or here, why ended asking own question. appreciated. thanks!

edit i'd able use generic name. example, right i'm doing this:

final imageview[] iv= new imageview[9];   iv[0]= (imageview)findviewbyid(r.id.imageview1); iv[1]= (imageview)findviewbyid(r.id.imageview2); iv[2]= (imageview)findviewbyid(r.id.imageview3); iv[3]= (imageview)findviewbyid(r.id.imageview4); iv[4]= (imageview)findviewbyid(r.id.imageview5); iv[5]= (imageview)findviewbyid(r.id.imageview6); iv[6]= (imageview)findviewbyid(r.id.imageview7); iv[7]= (imageview)findviewbyid(r.id.imageview8); iv[8]= (imageview)findviewbyid(r.id.imageview9);  for(imageview i: iv)         i.setonclicklistener(ivcl);//ivcl view.onclicklistener class 

wouldn't have been helpful if use loop this:

for(x=1;x<10;x++){         iv[x-1]=(imageview)findviewbyid("r.id.imageview" + x); //i understand above method wrong, because findviewbyid takes integer parameter, wanted do. 

use v.setimageresource() instead of v.getid().setimageresource(). v.getid() gives integer number inside r.id list class. no more. not possible set setimageresource integer class :) can view class. question:

imageview img = (imageview) v;     img.setimageresource(r.drawable.cross); 

for second question not think idea. try this: r.drawable.class.getfield("cross").getint(null)

i've tested worked on device:

private void test() {         try {             int ar1 = r.id.class.getfield("ar1").getint(null);             log.d(tag,ar1+"");         } catch (illegalaccessexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (illegalargumentexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (nosuchfieldexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -