如何在Android中的数据库中使用ListView中的图像?

时间:2010-09-08 08:13:30

标签: android listview listviewitem

我有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" />

...

列表项的布局:

...

<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" />

...

使用ListView从数据库填充setListAdapter(SimpleCursorAdapter)。对于每一行,我想显示一个图像(文件夹图标)。但是,对于两个特定的行(假设我知道如何使用,例如,某种指示器来识别行),我想显示不同的图像。假设我有一个未确定数量的文件夹,并且文件夹名称旁边的每一行都会显示一个文件夹图标。我们还假设我有一个Word文档和一个Excel文件,恰好是每个文件中的一个,我想为每个文件显示适当的图像。

1 个答案:

答案 0 :(得分:0)

类似于stealthcopter写的内容,您需要扩展SimpleCursorAdapter并覆盖bindView()以实现您的图标选择算法。以下a free excerpt one of my books讨论了该技术。