listview - Android: How to change drawable depending on list item selected -


my problem: listview presented user displaying list of animals, , depending on item chosen, next activity present user image of animal chosen.

my approach far has been pass position of list item selected:

intent.putextra("position_on_list", position) 

and extras in next activity.

this confused. how can display picture dynamically depending on animal selected?i have tried if statement approach;

if(position == 0){    imageview icon = (imageview)view.findviewbyid(r.id.animalimg)    icon.setimageresource(r.drawable.tiger) } 

but leads error when run app.could point me in right direction on how might go doing correctly.

thank you.

as far error, without details error anything...

i think if statement fine, might want switch statement default: alternative.

switch (position){  case 0:  break;  default:  break; } 

another option putting images in arrays.xml resource file , creating typedarray, it's little cleaner , more android friendly.

the arrays.xml file this

<?xml version="1.0" encoding="utf-8"?>  <resources>         <string-array name="animals">         <item>@drawable/tiger</item>         <item>@drawable/bear</item>         <item>@drawable/...</item>     </string-array> </resources> 

and activity have this, , no switches or ifs

typedarray ar = getresources().obtaintypedarray(r.array.animals))); imageview icon = (imageview)view.findviewbyid(r.id.animalimg) icon.setimageresource(ar.getresourceid(position, -1)); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -