Issue
I have declared few integer values in an xml and need to use the values in a Class to define the array size of an object.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Default Object Count -->
<item format="integer" name="item1" type="integer">3</item>
<item format="integer" name="item2" type="integer">1</item>
<item format="integer" name="item3" type="integer">1</item>
</resources>
I am using above values in my class as follows
public class InitialiseObjects {
// For now static number of objects initialized
private String TAG = "INIT_OBJECTS";
int ITEM1_COUNT = R.integer.item1;
int ITEM2_COUNT = R.integer.item2;
int ITEM3_COUNT = R.integer.item3;
private Item1[] item1Objs = new Item1[ITEM1_COUNT];
private Item2[] item2Objs = new Item2[ITEM2_COUNT];
private Item3[] item3Objs = new Item3[ITEM3_COUNT];
}
I expect ITEM*_COUNT to be 3,1,1 respectively for items 1,2,3. However I get 2131034112, 2131034113, 2131034114 respectively
What's wrong here ?
Android 2.2 [API-8] is being used
Solution
R.integer.item1
is the ID of the resource, and thus a very big and arbitrary integer.
The value your looking for is getContext().getResources().getInteger(R.integer.item1);
Thus, you won't be able to get them in a static code.
You should use lazy initialization in your code :
private Item1[] item1Objs;
public Item1[] getItem1Array(Context context) {
if (item1Objs == null) {
int count = context.getResources().getInteger(R.integer.item1);
item1Objs = new Item1[count];
}
return item1Objs;
}
Answered By - Orabîg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.