0

I'm trying to iterate through all R.id to find all ImageView objects and change there properties. but Android studio gives me an error.

here is code

public void initializeChipsViewholdersArray (){
   for (int i = 1; i<10; i ++){
        String viewholderName = "chip_00"+i;
       int id_2 = R.id.class.getFields(viewholderName).getInt(0);//erorr here
   ImageView chipViewholder= (ImageView)findViewById(id);
           chipViewholder.setVisibility(View.INVISIBLE);
}

here is error

Error:(28, 33) error: method getFields in class Class cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class Class

The solution I choose has been discussed here How do I iterate through the id properties of R.java class?

Community
  • 1
  • 1
obaz
  • 25
  • 5
  • Class.getFields() doesn't take an argument. Did you mean getField().get(null)? – Doug Stevenson Mar 29 '16 at 06:38
  • `getFields()` return all the public fields inside the `R.id` class, which is a static inner class inside the `R` class, which is a Java auto generated class for Resources. That method, `getFields()`, doesn't take any argument – William Kinaan Mar 29 '16 at 09:15
  • 1
    Possible duplicate of [How do I iterate through the id properties of R.java class?](http://stackoverflow.com/questions/2941459/how-do-i-iterate-through-the-id-properties-of-r-java-class) – William Kinaan Mar 29 '16 at 09:19

1 Answers1

0

getFields() return all the public fields inside the R.id class, which is a static inner class inside the R class, which is a Java auto generated class for Resources. That method, getFields(), doesn't take any argument

You are looking for getField(), not getFields()

public void initializeChipsViewholdersArray (){
   for (int i = 1; i<10; i ++){
        String viewholderName = "chip_00"+i;
       int id_2 = R.id.class.getField(viewholderName).getInt(0);
   ImageView chipViewholder= (ImageView)findViewById(id);
           chipViewholder.setVisibility(View.INVISIBLE);
}
William Kinaan
  • 28,059
  • 20
  • 85
  • 118