gpt4 book ai didi

java - Android viewBinder 在 simpleCursorAdapter 上显示图像

转载 作者:行者123 更新时间:2023-12-01 14:39:28 25 4
gpt4 key购买 nike

我已经创建了一个工作 ViewBinder 来与我的 simpleCursorAdapter 一起使用,并且一切正常。所需的图像按其应有的方式显示等。但是当我测试代码时,我在 View 绑定(bind)器中放置了一个日志,其中显示了显示图像的标准。当我查看 logCat 时,它显示了结果的两次迭代,如下所示。 (只有五个条目)。这将反过来创建我的 if 语句和结果图像显示的两次迭代。这不是显示问题,但它会产生一些冗余,因为它会在 listView 中显示图像两次。

日志文件:

columnIndex=1  categoryIdentifier = Supplier
columnIndex=1 categoryIdentifier = Customer
columnIndex=1 categoryIdentifier = Other
columnIndex=1 categoryIdentifier = Other
columnIndex=1 categoryIdentifier = Other
columnIndex=1 categoryIdentifier = Supplier
columnIndex=1 categoryIdentifier = Customer
columnIndex=1 categoryIdentifier = Other
columnIndex=1 categoryIdentifier = Other
columnIndex=1 categoryIdentifier = Other

运行viewBinder的代码是这样的:

代码:

private void fillData() {

//The desired columns to be bound:
String[] from = new String[] { ContactsDB.COLUMN_CATEGORY, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
//The XML views that the data will be bound to:
int[] to = new int[] {R.id.contact_icon, R.id.label2, R.id.label};

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.contact_row, null, from, to, 0);
// Set the ViewBinder to alternate the image in the listView
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
// int viewId = view.getId();
int categoryIndex = cursor.getColumnIndexOrThrow(ContactsDB.COLUMN_CATEGORY);

if(columnIndex == categoryIndex)
{
String categoryIdentifier = cursor.getString(columnIndex);
//switch categoryIdentifier
if(categoryIdentifier.equalsIgnoreCase("Supplier")){
displayImage = (ImageView) view;
displayImage.setImageResource(R.drawable.supplier);
}
if(categoryIdentifier.equalsIgnoreCase("Other")){
displayImage = (ImageView) view;
displayImage.setImageResource(R.drawable.other);
}
Log.v("TEST COMPARISON", "columnIndex=" + columnIndex + " categoryIdentifier = " + categoryIdentifier);

return true;
}
return false;
}
});

setListAdapter(adapter);
} // end of fillData

// Sort the names by last name, then by first name
String orderBy = ContactsDB.COLUMN_LAST_NAME + " COLLATE NOCASE ASC"
+ "," + ContactsDB.COLUMN_FIRST_NAME + " COLLATE NOCASE ASC" ;

// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//String[] projection = { ContactsDB.ROW_ID, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
String[] projection = { ContactsDB.ROW_ID, ContactsDB.COLUMN_CATEGORY, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
CursorLoader cursorLoader = new CursorLoader(this,
whateverContentProvider.CONTENT_URI, projection, null, null, orderBy);
return cursorLoader;
}

有人知道为什么会有这种冗余吗?

最佳答案

将 ListView android:layout_height 更改为 match_parent

关于java - Android viewBinder 在 simpleCursorAdapter 上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com