Android:我的GridView照片是混合的

时间:2013-08-28 15:12:29

标签: android gridview baseadapter

public class ImageAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ImageView photo;
    CheckBox CHECK;
    BitmapFactory.Options options;

    public ImageAdapter(Context c) {
        inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public int getCount() {
        return receivedphoto.size();
    }
    public Object getItem(int position) {
        return receivedphoto.get(position);
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)  {
            convertView = inflater.inflate(R.layout.phototab_list, parent, false);
            photo = (ImageView)convertView.findViewById(R.id.PHOTOS);
            CHECK = (CheckBox)convertView.findViewById(R.id.PHOTOSCHECK);
            options = new BitmapFactory.Options();
            options.inSampleSize = 16;
        }
        Log.i("LOAD", position + "");
        if(ReceivePhotos.get(position) == null)  {
            ReceivePhotos.set(position, BitmapFactory.decodeFile((String)(receivedphoto.get(position)), options));
        }   
        photo.setImageBitmap(ReceivePhotos.get(position));
        int check = Photoischeck.get(position);
        if(check == 0)  {
            CHECK.setChecked(false);
        } else {
            CHECK.setChecked(true);
        }
        return convertView;
    }
}

我使用ArrayList作为GridView数据。 但是当我向下滚动并向上滚动时,我的照片混合在一起。 混合意味着照片的位置发生了变化。 这是为什么? 拜托,帮帮我ㅠㅜ

1 个答案:

答案 0 :(得分:0)

试试这个...使用View holder来保存详细信息...

public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if(convertView == null)  {
            view = inflater.inflate(R.layout.phototab_list, parent, false);
            holder = new ViewHolder();
            holder.photo = (ImageView)convertView.findViewById(R.id.PHOTOS);
            holder.CHECK = (CheckBox)convertView.findViewById(R.id.PHOTOSCHECK);
            holder.options = new BitmapFactory.Options();
            holder.options.inSampleSize = 16;
        }else {
     holder = (ViewHolder) view.getTag();
}
        Log.i("LOAD", position + "");
        if(ReceivePhotos.get(position) == null)  {
            ReceivePhotos.set(position, BitmapFactory.decodeFile((String)(receivedphoto.get(position)), options));
        }   
        holder.photo.setImageBitmap(ReceivePhotos.get(position));
        int check = Photoischeck.get(position);
        if(check == 0)  {
            holder.CHECK.setChecked(false);
        } else {
            holder.CHECK.setChecked(true);
        }
        return view;
    }