从其他变量值做变量名 [英] make variable name from other variable value

查看:141
本文介绍了从其他变量值做变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要自动执行的过程

Basically I'm trying to automate process of

mDrawable[0] = this.getResources().getDrawable(R.drawable.img0);
mDrawable[1] = this.getResources().getDrawable(R.drawable.img1);
mDrawable[2] = this.getResources().getDrawable(R.drawable.img2);
mDrawable[3] = this.getResources().getDrawable(R.drawable.img3);
mDrawable[4] = this.getResources().getDrawable(R.drawable.img4);

一个循环:

for(int i = 0;i<=15;i++)
{       
mDrawable[i] = this.getResources().getDrawable(R.drawable.**img+i**);       
}

那么,如何弥补这一资源名称的 IMG4 循环变量

谢谢!

推荐答案

我相信你想实现这个样的例子。

I am sure you want to implement this kind of example.

当你想实现findViewById()在循环中当有与就像反varialbe 1,2,3,4,5等。

This is helpful when you want to implement findViewById() inside a loop when there are many number of views with just a difference like "counter" varialbe 1, 2,3,4,5,etc.

for(int i=0; i<15; i++) 
{
    String imageID = "img" + (i);
    int resID = getResources().getIdentifier(imageID, "drawable", getPackageName());

  /* if you want to reference any widgets like Button, TextView, ImageView, etc. then write below code:

    //int resID = getResources().getIdentifier(imageID, "id", getPackageName());   
  */
}

这篇关于从其他变量值做变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆