gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:38 24 4
gpt4 key购买 nike

我用来将图像从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com