在我的代码中,我有许多这些代码块...例如:
if(Avatar==1) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar1);
if(Avatar==2) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar2);
if(Avatar==3) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar3);
if(Avatar==4) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar4);
if(Avatar==5) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar5);
if(Avatar==6) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar6);
if(Avatar==7) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar7);
if(Avatar==8) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar8);
if(Avatar==9) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar9);
if(Avatar==10) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar10);
if(Avatar==11) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar11);
if(Avatar==12) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar12);
if(Avatar==13) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar13);
if(Avatar==14) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar14);
if(Avatar==15) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar15);
if(Avatar==16) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar16);
if(Avatar==17) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar17);
if(Avatar==18) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar18);
if(Avatar==19) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar19);
if(Avatar==20) ((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.avatar20);
例如在PHP中我可以写:
**$STRINGavatar** = "avatar20";
echo"((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(R.drawable.**$STRINGavatar**)";
但在Java中,它不起作用: - (
答案 0 :(得分:3)
你可以做点什么
getContext().getResources().getIdentifier("avatar" + i, "drawable", getContext().getPackageName())
根据名称获取资源。
[编辑]
然后你的代码就是:
int resId = getContext().getResources().getIdentifier("avatar" + Avatar, "drawable", getContext().getPackageName());
((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(resId);
你的变量Avatar应该以小写字母开头
答案 1 :(得分:1)
首先,对于你明显复制和粘贴的部分使用一个函数。
if (Avatar==1) setImage(R.drawable.avatar1);
if (Avatar==2) setImage(R.drawable.avatar2);
if (Avatar==3) setImage(R.drawable.avatar3);
//...
private void setImage(final int resource)
{
((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(resource);
}
至少水平收缩它并消除了大量重复。
接下来的事情是将整数和资源之间的映射移动到一个地方,特别是如果您需要多次查询:
private int getResource(final int avatar)
{
switch(avatar)
{
case 1: return R.drawable.avatar1;
case 2: return R.drawable.avatar2;
case 3: return R.drawable.avatar3;
//...
default:
throw new RuntimeException("No avatar for this");
}
}
然后您可以将上述代码更改为:
setImage(getResource(avatar));
答案 2 :(得分:0)
你应该尝试用不同的行
ImageView iv = (ImageView) dialogPopup.findViewById(R.id.imgView);
iv.setImageResource(R.drawable.avatar20)
答案 3 :(得分:0)
您可以将getIdentifier用于drawable
int id = getResources().getIdentifier(imageName, "drawable", getPackageName());
答案 4 :(得分:0)
动态获取资源ID: -
int id = getResources().getIdentifier("avatar" + Avatar, "drawable", getPackageName());
((ImageView)(dialogPopup.findViewById(R.id.imgView))).setImageResource(id);
答案 5 :(得分:0)
试试这个,使用枚举结构如下
<ul class="timeline">
<li><a href="">asd as</a></li>
<li><a href="">werwer we</a></li>
<li><a href="">werwer we</a></li>
</ul>