Ron Davis
Ron Davis

Reputation: 346

Dynamic Loading Image View

Hi I am trying to dynamically load an ImageView in a ListView this code doesnt seem to work.

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);
}

Log

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)

Upvotes: 0

Views: 702

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

No need to add "@drawable/" before drawable name.Get drawable id using getIdentifier :

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

Upvotes: 1

Related Questions