android - 在android中的ListView上显示不同的图标

标签 android listview icons

我有一个 xml 文件名“list_row.xml”,这是在 listView 中加载的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"
    android:src="@drawable/icon_one"
/>
<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
/>

我使用一个函数从表中加载数据并填充我的“list_row.xml”文件

private void fillData() {       
    Cursor c = mDbHelper.fetchAllNotes();
    String[] from = new String[]{ ListDbAdapter.KEY_ROWID,
            ListDbAdapter.KEY_ICON, ListDbAdapter.KEY_LABEL };
    int[] to = new int[]{ R.id.id, R.id.icon, R.id.label };
    SimpleCursorAdapter adapter = 
        new SimpleCursorAdapter(this, R.layout.list_row, c, from, to );     
    setListAdapter(adapter);
}

我的问题是如何检查图标值以设置 ListView 上的图标显示(我的数据必须为图标:icon_one、icon_two)。有人可以帮我编写代码吗?

最佳答案

来自documentation :

Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.

因此,重写 setViewImage() 并手动将您的图标与 ImageView 关联起来。或者,重写 newView()bindView() 并手动绑定(bind)整个行。

关于android - 在android中的ListView上显示不同的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1276706/

相关文章:

icons - 移动网站图标

java - 未找到默认 Activity 正在等待进程

Android POI 读取 Microsoft Word 文档时出错 : Provider com. bea.xml.stream.EventFactory 未找到

java - Android ArrayList 的自定义对象问题

android - ListView 适配器中的图像

html - 使用图标字体的替代方法

java - 在android中获取运行时权限

java - 读取联系人时应用程序崩溃

c++ - 如何在 Blackberry Cascades、QML 和 C++、QT 的 ListView 中获取 Web View

java - 如何更改实习生 Android 绘图的颜色?