Is there a way I could set an image to an ImageView
inside a ListView
? In this, I am using a SimpleCursorAdapter
to display all the fields and using a ViewBinder
to set the image bitmap to the ImageView
. The image is downloaded using an AsyncTask
. The code that I have written is provided below:
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);
}
}
Is there a way to set the image to the correct row with this method?
With the code that I currently have, the images are downloaded successfully. However, the images are being set randomly.
I created a custom class that inherits from SimpleCursorAdapter
, like Serdar Dogruyol mentions. Inside this custom class I @Override bindView
. Inside bindView
I call the super
, then get a handle to my ImageView
using view.findViewById
, with view
being one of the parameters passed into bindView
.
您应该创建自己的适配器,该适配器将继承自SimpleCursorAdapter,因此您可以使用自定义设计制作自己的ListView项目,并为其设置自定义布局,您可以在其中添加所需的任何Ui元素,包括ImageView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.