开发者

Set images in ListView

I have a listActivity. In each row of the list a name of a category is placed.

I download the activities from the web, so they may change occasionally. But I would like to set a image to each开发者_运维知识库 category.

I have a list of categories that may occur. And I have graphics for all categories. How can I match the correct image with corresponding category in a smooth way?

EDIT:

I have the pictures. And now how to add them to a listView. But Im looking for advice how to map a correct category to a correct picture. Could a bitmap factory do the trick, if I name the pictures to the same as the categories.

Bitmap bMap = BitmapFactory.decodeFile("/categoryicon_"+c.getId() + ".png");
                                imageView.setImageBitmap(bMap);


I would try a map with the category names as key and the resource id of the image as value. Then you can load a drawable from the resources using the right id.


For displaying the activities, I would use a custom array adapter, there are so many different examples out there so you will find loads if you just google. List14 is a really good example to start with. Then you have an array of activities, each activity has an id which decides what kind of image that will be shown, and of course the name of the activity. Then just depending on which id the activity has display the correct image.

If you would like to change it occasionally with refresh from a homepage you should probably download the images and store them in a good manner, if the image changes with the activities of course.


I would try and a map with the category names. Then i would use the resource id of the image as the value. Then you could load the images using the right ID.

If you want to add a imageview to a ListView you will also need to use a custom array adapter

This is an example of how you would do it.

This is a good one to get you started

http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications


Solved it like this:

String icon = "category_"+c.getId();
                                int resId = getResources().getIdentifier(icon, "drawable", getPackageName());
                                Bitmap bMap = BitmapFactory.decodeResource(getResources(), resId);
                                imageView.setImageBitmap(bMap)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜