-1

I'm trying to convert a c# windows form program over to android and I'm at the very last piece that I can't seem to figure out the translation for.

I have 9 buttons in a framelayout that I need to remove the text from either by iterating or by grabbing all at once.

In my original program I used a foreach loop like this:

foreach(control boardPosition in gameBoard)
{
    ((Button) boardPosition).Text ="";
    ((Button) boardPosition).ForeColor = Color.Black;
}

This is what I've gotten so far

FrameLayout GameBoard = (FrameLayout) findViewById(R.id.GameBoard)

for(Button boardPosition : GameBoard)
{
    boardPosition.setText("");
    boardPosition.setTextColor(Color.BLACK);        
}

The error that I'm receiving is just "foreach not applicable to type 'android.widget.Framelayout' "But I'm not sure whats it's replacement or if it has one.

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

0

The object you are looping against must implement iterable interface. Java must know that it can be iterated in some kind. FrameLayout is not iterable. It doesn't know your intentions that you have array of buttons in it.

For looping every button in layout I would use something like this:

    FrameLayout layout = (FrameLayout) view.findViewById(R.id.frame);
    int children = layout.getChildCount();
    for (int i = 0; i < children; i++) {
        View view = layout.getChildAt(i);
        if (view instanceof Button) {
            ((Button) view).setText("");
            ((Button) view).setTextColor(Color.BLACK);
        }
    }

If you still want to use foreach cycle, you must extend FrameLayout class and implement Iterable interface. But it's more work over nothing.

Michal
  • 1,008
  • 1
  • 12
  • 16