【问题标题】:Display different icon on ListView in android在android中的ListView上显示不同的图标
【发布时间】:2010-11-19 14:06:48
【问题描述】:

我有一个 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);
}

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

【问题讨论】:

    标签: android listview icons


    【解决方案1】:

    来自documentation

    绑定分两个阶段进行。第一的, 如果 SimpleCursorAdapter.ViewBinder 是 可用的, setViewValue(android.view.View, android.database.Cursor, int) 是 调用。如果返回值为 是的,绑定已经发生。如果 返回值为 false 和视图 绑定是一个TextView, setViewText(TextView, String) 是 调用。如果返回值为 false 并且要绑定的视图是 ImageView, setViewImage(ImageView, String) 被调用。

    因此,覆盖 setViewImage() 并手动将您的图标与 ImageView 关联。或者,覆盖 newView()bindView() 并手动绑定整行。

    【讨论】:

    • 为什么我觉得很难理解?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多