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.
You should make your own adapter which shall inherit from SimpleCursorAdapter and therefore you can make your own ListView item with your custom design and set a custom layout for it in which you can add whatever Ui element that you want including ImageView