Customizing an image in a ListView using SimpleCursorAdapter and ViewBinder

Is there a way to set an image on an ImageView inside a ListView ? In this case, I use SimpleCursorAdapter to display all the fields and using the ViewBinder to set the bitmap of the image in the ImageView . Image is uploaded using AsyncTask . The code I wrote is 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 line using this method?

With the code that I have, the images load successfully. However, images are set randomly.

+4
source share
2 answers

You have to make your own adapter, which should inherit from SimpleCursorAdapter, and therefore you can create your own ListView element with your own design and set your own layout for it, in which you can add any Ui element that you want, including ImageView

+2
source

I created my own class that inherits from the SimpleCursorAdapter , as Serdar Dogruyol mentions. Inside this custom class, I have an @Override bindView . Inside bindView I call super and then get the handle to my ImageView with view.findViewById , where view is one of the parameters passed to bindView .

+2
source

Source: https://habr.com/ru/post/1381095/


All Articles