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

时间:2015-12-05 12:49:34

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

我用来将SD卡中的图像显示到GridView 的ImageAdapter代码会导致图像重复。在GridView中重复了相同的一组图像,例如其中的10个。

这是我的适配器代码:

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

我的代码人员出了什么问题?

1 个答案:

答案 0 :(得分:0)

如果视图已被回收且 convertView 非空,则表示您未重置图像URI。这就是为什么你只看到必须从头开始创建视图的图像,因为 convertView null

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

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