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 <[email protected]>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:
> >
> > Description Resource Path Location Type
> > The type com.pingo.R$Id cannot be resolved. It is indirectly referenced
> > from required .class files Main.java /Pingo/src/com/pingo line
> > 1 Java 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en