There are many buttons for activating: button1, button2 ... button30 with id buttonCard1, buttonCard2, etc. There is a SQL base for 30 positions, that is, each button has a value of 1.2 or 3. The task is this: through the for loop, move these values ​​from the base in order and set the background for those buttons that are set to 1. I get the string value id, but I don’t understand how to apply a background to it.

for(int i = 1; i <= 30; i++){ userCursor = db.rawQuery("select * from " + DbDataHelper.TABLE + " where " + DbDataHelper.COLUMN_ID + "=?", new String[]{String.valueOf(i)}); color = userCursor.getInt(3); if(color == 1){ String id = "R.id.buttonCard" + i; //На полученный id нужно применить background ???(Integer.valueOf(id)).setBackgroundColor(getResources().getColor(R.color.colorButton1)); } } 

    1 answer 1

    The fact is that a string with a name cannot be directly converted to a resource ID. The code may be approximately the same (for the activation code that has the findViewById () method):

     if(color == 1){ int id = getResources().getIdentifier("buttonCard" + i, "id", getPackageName()); Button btn = (Button) findViewById(id); btn.setBackgroundColor(getResources().getColor(R.color.colorButton1)); }