开发者

Android: multiple icon in ListView

In my List开发者_如何学JAVA View, I want to display multiple Icon. Icon1 for list row 1, Icon 2 for list row 2, Icon1 for list row 3.


Override getView in your ListAdapter and then you can provide whatever layout you want for each row.

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/


You want to use a different icon in each row correct? Well you can use an Integer array of resource IDs of all your pictures and put those in order for your row items. The typical way people do this is hard code the list and specify the R.drawable.iconName that is in your resources. like:

int[] mapPic = {
      R.drawable.icon1,R.drawable.icon2, 
      R.drawable.icon3,R.drawable.icon4};

To me this is a crude solution, hard-coding is a big no-no. A more elegant way is to put the array into your xml file like string.xml or one you create for all your arrays. In your string.xml put in:

<array name="arrayIcon">
    <item>@drawable/icon1</item> 
    <item>@drawable/icon2</item> 
    <item>@drawable/icon3</item> 
    <item>@drawable/icon4</item> 
</array>

Then in your code when you first create your view before the setting content, add in:

TypedArray mapPic = getResources().obtainTypedArray(R.array.arrayIcon);

You can the access the object which is actually the drawable by:

Drawable drawable = typeArray.getDrawable(RowID);

In my code to set it in my view I had to grab the ViewWrapper and use getIcon() to set it.

ViewWrapper wrapper.getIcon().setImageDrawable(drawable);

And that is how you add your image resources you want to your list by using an array in the xml resources. Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜