我有一个 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/