1

I'm trying to create a chess game for Android and would like to avoid having to declare every button when they are all named so similarly. I tried to generate all of the A + Num button using this for loop.

int RowNum ;
for (RowNum = 1; RowNum < 8; RowNum++) {
    String Position = "A" + RowNum;
    Button Position = (Button)findViewById(R.id.Position);
}

But I have an error: Variable 'Position' is already defined in the scope.

I would really appreciate if someone could explain to me the best way to go about doing this.

Thanks in advance.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
BlackSpark
  • 13
  • 2

2 Answers2

0

You called your String and Button object both Position. Give them different names, e.g.

for (int RowNum = 1; RowNum < 8; RowNum++) {
    String currentPosition = "A" + RowNum;
    //Why doesn this not use the Position (now 'currentPosition' variable)?
    Button currentButton = (Button)findViewById(R.id.Position);
} 

Semantically I still don't get that piece of code because Position is not used within the argument of findViewById, to which you pass a static variable R.id.Position.

Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
  • I don't understand how this will produce separate references if the value of currentButton does not change, seeing as this will be the name of he reference? – BlackSpark Mar 25 '16 at 13:55
  • All I want to do is create the button with a name of Position, and it be linked to the button in the xml which has the same name, I.E. – BlackSpark Mar 25 '16 at 13:59
  • Button A1 = (Button)findViewById(R.id.A1); – BlackSpark Mar 25 '16 at 13:59
  • But what is the contents of the static variable `R.id.A1`? If it is`"A1"`, then you can pass `currentPosition` the function. In your loop, you have to regenerate those ID names and pass it into `findViewById()`, then you're good to go. And also save the found buttons to an array or something for later reference. – Maximilian Gerhardt Mar 25 '16 at 14:02
  • It's just the button for the bottom left corner of my chess board, with an ID of A1, anyways I'll give that a go, thanks for the feedback! – BlackSpark Mar 25 '16 at 14:10
0

R.Id.A1, R.id.A2, etc. are identifiers and each of them is mapped to a single integer during compilation of R class. Thus, you cannot manipulate the 'R.id.sth' as a string because it will not compile properly.

A way to solve that is search for the string in resources dynamically with a code like this:

Button[] buttons=new Button[8]; 
for(int RowNum=0; RowNum<8; RowNum++) {
   String buttonID = "A"+(RowNum+1); 
   int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
   buttons[RowNum] = ((Button) findViewById(resID));
}

Alternatively, to avoid the time overhead of dynamic search you can add a resource array:

int[] butIds = {R.id.A1, R.id.A2,...};

Button[] buttons= new Button[8]; 
    for(int RowNum=0; RowNum<8, RowNum++) {
       buttons[RowNum]= ((Button) findViewById(butIds[RowNum]));
}

You can even store the resource array in XML form and retrieve it as a TypedArray.

Community
  • 1
  • 1
Sevle
  • 3,109
  • 2
  • 19
  • 31
  • 1
    Thank you, I did try something similar to this but I could not get it to work, which, based on what you've shown I've realised why, I just manually created them in the end, but i might go back and implement this. Thanks again! – BlackSpark Mar 25 '16 at 15:33