Android 多次加载Gridview项

Android 多次加载Gridview项,android,gridview,baseadapter,Android,Gridview,Baseadapter,我在我的应用程序中为一个画廊活动创建了一个GridView,当活动第一次启动时,我注意到了奇怪的行为。我包括了一个print语句,它告诉我何时加载每个图像,这是我得到的输出: 01-30 17:27:09.597 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0 01-30 17:27:09.607 25390-25390/com.thoma

我在我的应用程序中为一个画廊活动创建了一个GridView,当活动第一次启动时,我注意到了奇怪的行为。我包括了一个print语句,它告诉我何时加载每个图像,这是我得到的输出:

01-30 17:27:09.597 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.607 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.607 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.607 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_07_146 into slot 1
01-30 17:27:09.707 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_34_369 into slot 2
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_38_530 into slot 3
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_24_45_214 into slot 4
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.717 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
01-30 17:27:09.787 25390-25390/com.thomas.galleryapp I/System.out: Loaded gallery_img_2016_01_30_17_23_59_197 into slot 0
似乎无论有多少项被放入GridView,项0都会被加载多次,通常在15-25次之间。为什么它一直在重新加载第一个项目?以下是GridView的xml:

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:id="@+id/gridView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:numColumns="3"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:layout_centerHorizontal="true"
        android:gravity="center" />
以及网格的图像适配器:

private class ImageAdapter extends BaseAdapter{
    private LayoutInflater galleryItemInflater;

    public ImageAdapter() {
        galleryItemInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        ImageView imageView;
        if (convertView == null) {
            convertView = galleryItemInflater.inflate(R.layout.gallery_item, null);
        }
        imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
        if (fileList.size() > 0){
            imageView.setImageBitmap(fileList.get(position).getImage());
            System.out.println("Loaded " + fileList.get(position).getFileName() + " into slot " + position);
        }
        return convertView;
    }

    public int getCount(){
        return fileList.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
}

尝试在getView中记录位置以查看它是否为0,如果不是,则您的备份数据有问题。这意味着它包含dupe itemsTry public long getItemId(int position){return position;}@Elltz我在getView中记录了该位置,并且0@tiny我更改了getItemId方法以返回位置,但这并没有改变任何事情getView总是被多次调用。请尝试在getView中记录位置以查看它是否为0,如果不是,则支持数据有问题。这意味着它包含dupe itemsTry public long getItemId(int position){return position;}@Elltz我在getView中记录了这个位置0@tiny我更改了getItemId方法以返回位置,但这并没有改变任何东西getView总是被多次调用。
public class SquareImageView extends ImageView {

    public SquareImageView(final Context context)
    {
        super(context);
    }

    public SquareImageView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
    }

    public SquareImageView(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}
private class ImageAdapter extends BaseAdapter{
    private LayoutInflater galleryItemInflater;

    public ImageAdapter() {
        galleryItemInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(int position, View convertView, ViewGroup parent){
        ImageView imageView;
        if (convertView == null) {
            convertView = galleryItemInflater.inflate(R.layout.gallery_item, null);
        }
        imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
        if (fileList.size() > 0){
            imageView.setImageBitmap(fileList.get(position).getImage());
            System.out.println("Loaded " + fileList.get(position).getFileName() + " into slot " + position);
        }
        return convertView;
    }

    public int getCount(){
        return fileList.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
}