动态加载图像视图

时间:2014-12-22 19:02:02

标签: android imageview

您好我正在尝试在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)

1 个答案:

答案 0 :(得分:1)

无需在可绘制名称之前添加"@drawable/"。使用getIdentifier获取可绘制ID:

String uri = myImage[position];
int imageResource = mycontext.getResources().getIdentifier(uri, 
                                      "drawable",   mycontext.getPackageName());
 imageview.setImageResource(imageResource);