开发者

How to use an image in a ListView from a database in Android?

开发者 https://www.devze.com 2023-01-15 11:21 出处:网络
I have a开发者_JAVA百科 ListView: ... <ListView xmlns:android=\"http://schemas.android.com/apk/res/android\"

I have a开发者_JAVA百科 ListView:

...

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="?android:attr/colorBackground"
    android:cacheColorHint="?android:attr/colorBackground"
    android:choiceMode="singleChoice"
    android:drawSelectorOnTop="false"
    android:fadingEdge="none"
    android:fastScrollEnabled="true"
    android:scrollingCache="true" />

...

And a layout for the list items:

...

<ImageView
    android:id="@+id/folder_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="5dip"
    android:paddingTop="3dip"
    android:src="@drawable/icon_folder" />

<TextView
    android:id="@+id/fol_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/folder_image"
    android:textSize="22dip" />

...

The ListView is populated from a database using a setListAdapter(SimpleCursorAdapter). For each row, I want to display an image (a folder icon). However, for two specific rows (assume I know how to identify the rows using, for example, some sort of indicator) I'd like to display different images. Let's assume that I have an undetermined number of folders and a folder icon will be displayed for each row along side the folder's name. Let's also assume that I have, for example, a Word doc and an Excel file, exactly one of each, and I'd like to display the appropriate images for each.


Akin to what stealthcopter wrote, you will need to extend SimpleCursorAdapter and override bindView() to implement your icon-selection algorithm. Here is a free excerpt from one of my books that discusses the technique.

0

精彩评论

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

关注公众号