Question

I know there are a ton of getIdentifier questions asking a similar thing. I have tried numerous things, but I continue to get 0 as my result. I have a fragment, that fragment contains a table, and the table is full of "empty" views. I am attempting to find a view and fill it based on the row and col variables that get set in some pretty simple switch statements (excluded for clarity). I have the method:

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);

    int row;
    int col;

    ... methodology that sets row and count ...

    String idString = "R.id.row" + row + "_col" + col;
    int id = getResources().getIdentifier(idString, "id", activity.getBaseContext().getPackageName());
    int rID = R.id.row1_col1;

    Log.d(TAG, "id = " + id + " and rID = " + rID + " with " + idString + 
           " for row " + row + " x col " + col);
}

Where int id is supposed to be set to the resource id of a view based on it's row/col. If I test for row1, col1 I get the following log output

id = 0 and rID = 2131034203 with idString R.id.row1_col1 for row 1 x col 1

This tells me that the resource is there, that my row and col variables are getting set appropriately in my methodology, and that my string is set right for using getIdentifier (as far as I know). Why is it, then, that I always get 0 no matter what row and col I test with?

I have tried changing "id" to a different type, changing getResources() to activity.getResources(), using an explicit string without variables... nothing seems to work.

Was it helpful?

Solution

You don't pass in R.id.id_name, but just id_name:

String idString = "row" + row + "_col" + col;

As the second parameter tells the system you are looking for an id and the third tells the system what resource R file to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top