Generating bimaps with the instance title differen

2019-08-20 04:16发布

问题:

i need the generated instance name to be different every thime eg blocktitle1, blocktitle2, blocktitle3 and so on. I have put some code to change the string variable "title" but just putting the word "Title" where im making a new bitmap will make the instance called "Title" not eg "blocktitle2".

I would be very greatfull in anyone were to help.

 List<Block> blocks = new LinkedList<Block>();
 Random rnd = new Random(System.currentTimeMillis());

 int randomx = 400;
 public Block block;
 int blocknum = 10;
 String Title = "blocktitle" + blocknum;


public void generateBlocks(){

          if(blocknum > 0){

              int offset = rnd.nextInt(400) + 100; //500 is the maximum offset, this is a constant
              x += offset;                         //ofset will be between 100 and 400

              // i need the word below "block" to be recognised as a String variable "String Title"
             block = new Block(BitmapFactory.decodeResource(getResources(), R.drawable.block), randomx, 200);
              blocknum -= 1;


    }

}

回答1:

Try this:

private static final String titlePrefix = "blocknum";
private static int titleNo = 0;
public String getNextTitle() {
    return titlePrefix + titleNo++;
}