简体   繁体   English

使用SimpleCursorAdapter和ViewBinder在ListView项目中设置图像

[英]Setting an image in ListView item using SimpleCursorAdapter and ViewBinder

Is there a way I could set an image to an ImageView inside a ListView ? 有没有办法在ListView中将图像设置为ImageView In this, I am using a SimpleCursorAdapter to display all the fields and using a ViewBinder to set the image bitmap to the ImageView . 在这里,我使用SimpleCursorAdapter显示所有字段,并使用ViewBinder将图像位图设置为ImageView The image is downloaded using an AsyncTask . 使用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. 我创建了一个继承自SimpleCursorAdapter的自定义类,如Serdar Dogruyol提到的。 Inside this custom class I @Override bindView . 在这个自定义类里面我@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 . bindView里面我调用super ,然后使用view.findViewById获取我的ImageView view.findViewByIdview是传递给bindView的参数bindView

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SimpleCursorAdapter.ViewBinder更改listView中的颜色。 安卓 - Using SimpleCursorAdapter.ViewBinder to change color in a listView. android 使用ViewBinder,SimpleCursorAdapter,SQLite在ListView上显示相机拍摄的图片 - Using ViewBinder, SimpleCursorAdapter, SQLite, to show pictures taken by a camera on ListView 使用SimpleCursorAdapter.ViewBinder更改Listview中的文本颜色 - Changing text color in Listview using SimpleCursorAdapter.ViewBinder 将ViewBinder与TextView一起用于自定义ListView项 - Using ViewBinder for a custom ListView item with TextView 使用SimpleCursorAdapter和ViewBinder的Spinner项目单击侦听器的方法 - Method for Spinner item click listener with SimpleCursorAdapter and ViewBinder 使用SimpleCursorAdapter在ListView中设置图像 - Set image in a ListView using SimpleCursorAdapter 使用SimpleCursorAdapter.ViewBinder更改TextView的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of TextView 使用SimpleCursorAdapter.ViewBinder更改视图的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of View 使用ViewBinder从SimpleCursorAdapter中显示的自定义列表 - Customizing list shown from SimpleCursorAdapter using ViewBinder Android-使用SimpleCursorAdapter时在列表视图中设置edittext - Android - Setting edittext in a listview when using SimpleCursorAdapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM