How to get the id (int) of several layouts with variables listed in Android?

1

Good, I would like to know how to get the id of a series of layouts that have names of similar variables, in this case their suffix is a number.

I want to store your IDs in an array. But obviously I can not change the number by a concatenation of the number, because then the data I'm going to assign becomes a String.

I do not know what methods would give me the convenience of doing this, here I leave in code something similar to what I want to do but obviously it does not work for what I already mentioned.

int[] CASILLAS = new int[9];
for (int i = 0; i < 9;i++){
    CASILLAS[i] = R.id.imageCas+""+i; 
} 
    
asked by Parzival 20.06.2017 в 00:54
source

1 answer

1

Using what is perhaps obvious, is actually incorrect, since pairing R.id.imageCas+""+i will result in an incorrect value, even a value that might not be integer by length.

int valorId = Integer.parseInt(R.id.imageCas+""+i); 

The ids generated by each resource found in your project are of the integer type and are written inside the file R.java , as an example for the layouts you have:

 public static final class layout {
    public static final int activity_main=0x7f04001e;
    public static final int activity_alert=0x7f04001f;
    public static final int activity_configuration_alerts=0x7f040020;
    public static final int activity_contact=0x7f040021;
    public static final int activity_detail=0x7f040022;
    public static final int activity_email=0x7f040024;
    public static final int activity_gallery=0x7f040026;
    public static final int activity_main_bubble=0x7f040029;
 }

but these id's can change for that reason as a suggestion is to store the name of the layout or resource, within an array of integer values, for example:

private static final int[] mylayouts = new int[]{R.id.activity_main, R.id.activity_alert, R.id.activity_configuration_alerts, R.id.activity_contact, R.id.activity_detail, R.id.activity_email, R.id.activity_gallery, R.id.activity_main_bubble};
    
answered by 20.06.2017 в 19:57