0

Not sure how to describe this one so I'll just do my best. I have a ListView that always displays five strings whatever is selected. I've copied an example to place images at the left of each list item. However I have five separate images and I don't know how to set each respective image in the custom array adapter. The current code is as follows:

String s = values[position];
if (s.startsWith("Windows")) {
 imageView.setImageResource(R.drawable.image1);
} else {
 imageView.setImageResource(R.drawable.image2);
}

I want mine to do the following:

if index = 0 then setImageResource as image1; elseif index = 1 then setImageResource as image2, etc.

Can I do this?

asked Jul 6, 2014 at 13:28
0

2 Answers 2

1

If there are static values. Why you don't use switch statement. For example:

switch(position)
{
 case 0:
 imageView.setImage(R.drawable.image1);
 break;
 .
 .
 .
 .
 case 4:
 imageView.setImage(R.drawable.image5);
 break;
}
answered Jul 6, 2014 at 13:38

1 Comment

Excellent Arben. That's just what it needed.
0

Another clean solution would be creating a POJO of the your array item.

class Item{
 String text;
 int imageId;
 //respective setter getters
 }

In your array adapter,the getView method, modify it accordingly

answered Jul 6, 2014 at 13:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.