Issue
I have an 8x8 board with 64 four fields, each being a LinearLayout.
Each LinearLayout has its ID like (field1, field2... field64). Now I want to modify the background of each field later in my program but I don't know how to retrieve the layout I want to modify. I wanted to put all of them in the Array but it's not working.
private LinearLayout[] fields = new LinearLayout[65];
this.fields[field_id].setBackgroundColor(colour);
Another approach:
for (int i=1; i<65; i++) {
findViewById(R.id.field+i).setBackgroundColor(Color.WHITE);
}
It does not work as well (obviously). How should I approach this?
Solution
You can add them to a list, like this:
List fieldList = new ArrayList(64);
fieldList.add(R.id.field1);
fieldList.add(R.id.field2);
fieldList.add(R.id.field3);
// etc.
fieldList.add(R.id.field64);
Or use any other method to create such a list/array. It is a bit tedious, adding all fields like this, but it provides a usable list that can be used in loops.
It is also possible (I think), to renumber the ids of the fields (in R.java
). If you make them subsequent, you can use the method you tried in the example code of your question. I do not not for sure whether these IDs stay the same when R.java
is re-generated... If not, this method is not usable!
Answered By - Veger
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.