java - Problem with converting a string to an integer and passing as an ID to a view -


there 20 buttons in activity .

the ids r.id.buttonr1c1; r.id.buttonr1c2 .. , on ie. row 1, col 1..

now earlier had created 20 buttons.

private button b1,b2,b3...; 

and

b1=(button)findviewbyid(r.id.buttonr1c1); b2=(button)findviewbyid(r.id.buttonr1c2); 

.... , on.

finally

b1.setonclicklistener(this); b2.setonclicklistener(this);   

... 20

so thought i'd create button array

button barray[][]=new button{4][5]; for(int i=1;i<=4;i++) {    (int j=1;j<=5;j++) {       string t="r.id.buttonr"+i+"c"+j;       barray[i-1][j-1]=(button)findviewbyid(integer.parseint(t));    } } 

gives error..

any help??

if have copied code directly, syntax wrong.

button barray[][]=new button{4][5]; 

should be

button barray[][]=new button[4][5]; 

note curly bracket instead of square bracket before 4.

your next problem, trying value of r.id.something, have string representation of it. so, way know using reflection.

what need is,

button barray[][]=new button{4][5]; for(int i=1;i<=4;i++) {    (int j=1;j<=5;j++) {       class rid = r.id.getclass();       // gets id r.id object.       int id = rid.getfield("buttonr"+i+"c"+j).getint(r.id);       barray[i-1][j-1]=(button)findviewbyid(id);    } } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -