R.array is a class but not an object?
This is for Android, but I believe it's a general Java question, 开发者_开发问答so I'm labelling it as such.
I'm trying to use reflection on R.array to get an array from the list. The arrays are named stringArraySubCategoryX
where X is replaced with a number from 0 to 20. This is my code, but I ran into a problem:
Field subCatField = R.array.class.getField("stringArraySubCategory" + position);
subCatergorySpinner.setAdapter(ArrayAdapter.createFromResource(InfoActivity.this, subCatField.getInt(R.array), android.R.layout.simple_spinner_item));
The problem part is the subCatField.getInt(R.array)
bit, which is supposed to return the resource int value of the selected array. But Field.getInt(arg)
's argument is an Object. I checked R.array, and that says public static final class array
, but it seems that it's not accepted.
What am I doing wrong?
subCatField.getInt(null)
Is what you're looking for. Provided the stringArraySubCategoryX
is static
, of course.
However ... you state: "I'm trying to use reflection on R.array to get an array from the list."
How is that an int
?
subCatField.get(null)
will return the static Object
contained in the Field
精彩评论