【问题标题】:Android ImageAdapter repeating the same images/items again and againAndroid ImageAdapter 一次又一次地重复相同的图像/项目
【发布时间】:2015-12-05 12:49:33
【问题描述】:

我用来将图像从我的 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
            }
        });

我的代码人有什么问题?

【问题讨论】:

    标签: java android android-activity gridview android-adapter


    【解决方案1】:

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

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

    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));
        }
    }
    

    【讨论】:

    • 所以关于我的问题,如果我这样调用适配器: ImageAdapter adap = new ImageAdapter(MediaChoiceView.this, cursor); sdcardImages.setAdapter(adap);这是对的吗?因为我现在没有看到任何图像。 Gridview 不为空(我仍然可以滚动),但图像是黑色的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 2015-07-04
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多