4

有没有办法可以将图像设置为ImageView内部ListView?在此,我使用 aSimpleCursorAdapter显示所有字段并使用 aViewBinder将图像位图设置为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);
        }
    }

有没有办法使用这种方法将图像设置为正确的行?

使用我目前拥有的代码,图像已成功下载。但是,图像是随机设置的。

4

2 回答 2

2

您应该制作自己的适配器,该适配器应从 SimpleCursorAdapter 继承,因此您可以使用自定义设计制作自己的 ListView 项目并为其设置自定义布局,您可以在其中添加您想要的任何 Ui 元素,包括 ImageView

于 2011-11-14T09:54:07.140 回答
2

我创建了一个继承自的自定义类SimpleCursorAdapter,就像 Serdar Dogruyol 提到的那样。在这个自定义类中 I @Override bindView。在里面bindView我调用super,然后获取我的ImageViewusing的句柄view.findViewByIdview作为传入的参数之一bindView

于 2013-11-26T19:29:57.890 回答