Re: [android-developers] Getting hold of a resource using generated string

2010-01-18 Thread Mark Murphy
Try Resources#getIdentifier().

 Ok, here's the problem. I have a layout file with 25 ImageButtons in in
 laid out in a 5x5 grid. During my application setup, I need to go
 through and add onClick and onLongClick handlers to each button and add
 the buttons to a local array. The problem is, I don't want to explicitly
 name every button in code. It's error prone. I want to do this setup in
 a loop. I thought I could do something like the following:

 int buttonBaseId = com.pingo.R.Id.button00;
 ImageButton button = null;
 for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
 for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
 button = (ImageButton) findViewById(buttonBaseId++);
 setupButton(button,i,j);
 }
 }

 But that doesn't work. I get the error:

 DescriptionResourcePathLocationType
 The type com.pingo.R$Id cannot be resolved. It is indirectly referenced
 from required .class filesMain.java/Pingo/src/com/pingoline
 1Java Problem

 What I would prefer would be some way to get the buttons like:


 int buttonBaseId = com.pingo.R.Id.button00;
 ImageButton button = null;
 for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
 for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
 String idString = button + i + j;
 button = (ImageButton) findViewByIdString(idString);
 setupButton(button,i,j);
 }
 }

 Suggestions?

 Thanks,
   Ray


-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html


-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Getting hold of a resource using generated string

2010-01-18 Thread TreKing
Alternatively, you could just create and set up these 25 buttons in code.

for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
 for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
 ImageButton button = new ImageButton(this);
 setupButton(button,i,j);
 }
 }

-
TreKing - Chicago transit tracking app for Android-powered devices
http://sites.google.com/site/rezmobileapps/treking


On Mon, Jan 18, 2010 at 6:07 AM, Mark Murphy mmur...@commonsware.comwrote:

 Try Resources#getIdentifier().

  Ok, here's the problem. I have a layout file with 25 ImageButtons in in
  laid out in a 5x5 grid. During my application setup, I need to go
  through and add onClick and onLongClick handlers to each button and add
  the buttons to a local array. The problem is, I don't want to explicitly
  name every button in code. It's error prone. I want to do this setup in
  a loop. I thought I could do something like the following:
 
  int buttonBaseId = com.pingo.R.Id.button00;
  ImageButton button = null;
  for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
  for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
  button = (ImageButton) findViewById(buttonBaseId++);
  setupButton(button,i,j);
  }
  }
 
  But that doesn't work. I get the error:
 
  DescriptionResourcePathLocationType
  The type com.pingo.R$Id cannot be resolved. It is indirectly referenced
  from required .class filesMain.java/Pingo/src/com/pingoline
  1Java Problem
 
  What I would prefer would be some way to get the buttons like:
 
 
  int buttonBaseId = com.pingo.R.Id.button00;
  ImageButton button = null;
  for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
  for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
  String idString = button + i + j;
  button = (ImageButton) findViewByIdString(idString);
  setupButton(button,i,j);
  }
  }
 
  Suggestions?
 
  Thanks,
Ray


 --
 Mark Murphy (a Commons Guy)
 http://commonsware.com
 Android App Developer Books: http://commonsware.com/books.html



 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Getting hold of a resource using generated string

2010-01-18 Thread Ray Benjamin


Thank you. I knew there had to be a way.

Mark Murphy wrote:

Try Resources#getIdentifier().

  

Ok, here's the problem. I have a layout file with 25 ImageButtons in in
laid out in a 5x5 grid. During my application setup, I need to go
through and add onClick and onLongClick handlers to each button and add
the buttons to a local array. The problem is, I don't want to explicitly
name every button in code. It's error prone. I want to do this setup in
a loop. I thought I could do something like the following:

int buttonBaseId = com.pingo.R.Id.button00;
ImageButton button = null;
for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
button = (ImageButton) findViewById(buttonBaseId++);
setupButton(button,i,j);
}
}

But that doesn't work. I get the error:

DescriptionResourcePathLocationType
The type com.pingo.R$Id cannot be resolved. It is indirectly referenced
from required .class filesMain.java/Pingo/src/com/pingoline
1Java Problem

What I would prefer would be some way to get the buttons like:


int buttonBaseId = com.pingo.R.Id.button00;
ImageButton button = null;
for (int i = 0; i  R.attr.GRID_SIZE; ++i) {
for (int j = 0; j  R.attr.GRID_SIZE; ++j) {
String idString = button + i + j;
button = (ImageButton) findViewByIdString(idString);
setupButton(button,i,j);
}
}

Suggestions?

Thanks,
  Ray




  


--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en