您好我正在尝试在ListView中动态加载ImageView,此代码似乎无法正常工作。
public View getView(int position, View convertView, ViewGroup parent) {
...
String uri = "@drawable/"+myImage[position];
int imageResource = mycontext.getResources().getIdentifier(uri, null, mycontext.getPackageName());
ImageView imageview= (ImageView) convertView.findViewById(R.id.imgPic);
Drawable res = mycontext.getResources().getDrawable(imageResource);
imageview.setImageDrawable(res);
}
日志
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1195)
at android.content.res.Resources.getDrawable(Resources.java:729)
at android.content.res.Resources.getDrawable(Resources.java:711)
at parser.my.myapplication.NotificationAdapter.getView(NotificationAdapter.java:77)
答案 0 :(得分:1)
无需在可绘制名称之前添加"@drawable/"
。使用getIdentifier
获取可绘制ID:
String uri = myImage[position];
int imageResource = mycontext.getResources().getIdentifier(uri,
"drawable", mycontext.getPackageName());
imageview.setImageResource(imageResource);