davidbain
davidbain

Reputation: 2661

Using a variable as a method call

I'm trying to use an if statement to increment through a large number of ImageViews. The naming convention is designed so I can increment it as seen below (example file name r2c1). I need help in then setting this variable as the R.drawable in the setBackgroundResource. I've tried a few ideas to no avail. Thanks.

if (column == 0){
        String adjustment = "r"+col1drop+"c1"; 
        View Row1Adjust = (ImageView)this.findViewById(R.id.r6c2);
        Row1Adjust.setBackgroundResource(R.drawable.adjustment);
        col1drop++; //declared earlier on 
    }

Upvotes: 1

Views: 54

Answers (1)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this way

if (column == 0){
            String adjustment = "r"+col1drop+"c1"; 
            View Row1Adjust = (ImageView)this.findViewById(R.id.r6c2);
            int resourceId = getResources().getIdentifier(adjustment, "drawable", getPackageName());
            Row1Adjust.setBackgroundResource(resourceId);
            col1drop++; 
        }

Upvotes: 1

Related Questions