java - Android ImageAdapter 一次又一次地重复相同的图像/项目

标签 java android android-activity gridview android-adapter

我用来将图像从 SD 卡显示到 GridView 的 ImageAdapter 代码导致图像重复。同一组图像(例如 10 个)在 GridView 中重复。

这是我的适配器代码:

private class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context localContext) {
            context = localContext;
        }
        public int getCount() {
            return cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                picturesView.setPadding(0, 0, 0, 0);
                picturesView.setLayoutParams(new GridView.LayoutParams(300, 300));
            }
            else {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }

此外,这里是调用适配器的代码,以便在 GridView 中显示我的所有 SD 卡图像

String[] projection = {MediaStore.Images.Thumbnails._ID};
        // Create the cursor pointing to the SDCard
        cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,
                MediaStore.Images.Thumbnails._ID);
        // Get the column index of the Thumbnails Image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        sdcardImages.setAdapter(new ImageAdapter(this));
        // Set up a click listener
        sdcardImages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                // Get the data location of the image
                String[] projection = {MediaStore.Images.Media.DATA};
                cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        projection, // Which columns to return
                        null,       // Return all rows
                        null,
                        null);
                columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToPosition(position);
                // Get image filename
                String imagePath = cursor.getString(columnIndex);
                // Use this path to do further processing, i.e. full screen display
            }
        });

我的代码有什么问题吗?

最佳答案

如果 View 被回收并且 convertViewnot null,则您不会重置图像 URI。这就是为什么您只看到必须从头开始创建 View 的图像,因为 convertViewnull

因此,如果需要,可以重用或创建新 View :

final ImageView picturesView = convertView == null ? new ImageView(context) : (ImageView) convertView;

然后根据需要进行配置。

但是在您的情况下,从 CursorAdapter 而不是 BaseAdapter 抽象类继承行为要容易得多:

private class ImageAdapter extends CursorAdapter {
    private final int columnIndex;
    public ImageAdapter(Context context, Cursor cursor) {
        super(context, cursor, true);
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return new ImageView(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final ImageView picturesView = (ImageView) view;
        final int imageID = cursor.getInt(columnIndex);
        final Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageID));
        picturesView.setImageURI(uri);
        picturesView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        picturesView.setPadding(0, 0, 0, 0);
        picturesView.setLayoutParams(new GridView.LayoutParams(300, 300));
    }
}

关于java - Android ImageAdapter 一次又一次地重复相同的图像/项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34105086/

相关文章:

java - 调用 DestroyJavaVM 后重新调用 JNI_CreateJavaVM 返回 -1

java - 制作一个有光泽的 JFrame 和 JDialog

android - GPS 定位始终返回零速度

android - 构建后台 Activity (Android)

android - 如何显示两个 Activity 之间的进度对话框?

java - 使用java中的JOptionPane是否可以同时显示多个弹出窗口?

java - 将经过的毫秒数转换为正确的 Java 日期格式?

android - 如何在 Android 中对齐表格内容

java - 当我按下按钮时应用程序强制关闭?安卓

android - 启动 Activity 的正确方法是什么?