[英]Using SimpleCursorAdapter.ViewBinder to change color in a listView. android
[英]Setting an image in ListView item using SimpleCursorAdapter and ViewBinder
有没有办法在ListView
中将图像设置为ImageView
? 在这里,我使用SimpleCursorAdapter
显示所有字段,并使用ViewBinder
将图像位图设置为ImageView
。 使用AsyncTask
下载映像。 我写的代码如下:
private void updateTimelineUI() {
Cursor data = dbHelper.query(Constants.TABLE_NAME, null, null);
if (data.moveToFirst()) {
adapter = new SimpleCursorAdapter(this, R.layout.tweet_row, data, new String[] {Constants.CREATED_TIME, Constants.USERNAME, Constants.PROFILE_IMAGE, Constants.TWEET}, new int[] {R.id.time, R.id.username, R.id.userImageView, R.id.tweetMessage});
SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view != null && view.getId() != R.id.userImageView) {
return false;
}
String imageUrlString = cursor.getString(columnIndex);
String username = cursor.getString(cursor.getColumnIndex(Constants.USERNAME));
String path = getDir("images", MODE_PRIVATE).getAbsolutePath() + "/" + username + ".png";
ImageDownloader downloader = new ImageDownloader();
downloader.setImageUrlString(imageUrlString);
downloader.setImageView(view);
downloader.setFilePath(path);
downloader.execute(imageUrlString);
return true;
}
};
int index = data.getColumnIndex(Constants.PROFILE_IMAGE);
//Log.d(Constants.TAG, "" + index);
adapter.setViewBinder(viewBinder);
viewBinder.setViewValue(userImageView, data, index);
timelineList.setAdapter(adapter);
}
}
有没有办法用这种方法将图像设置为正确的行?
使用我目前拥有的代码,可以成功下载图像。 但是,图像是随机设置的。
我创建了一个继承自SimpleCursorAdapter
的自定义类,如Serdar Dogruyol提到的。 在这个自定义类里面我@Override bindView
。 在bindView
里面我调用super
,然后使用view.findViewById
获取我的ImageView
view.findViewById
, view
是传递给bindView
的参数bindView
。
您应该创建自己的适配器,该适配器将继承自SimpleCursorAdapter,因此您可以使用自定义设计制作自己的ListView项目,并为其设置自定义布局,您可以在其中添加所需的任何Ui元素,包括ImageView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.