1

imagepaths我从 sqlite获取 data( ),所以我曾经simplecursoradaptergridview. 在每个 gridview 项目中,它包含一个 imageview 和一个复选框,如下面的代码所示。在这里,我的第一个问题是,每当滚动网格视图并在一段时间后在自己的位置安顿下来时,图像似乎会改变它们的位置,这看起来很奇怪。我认为这些问题是因为视图在滚动时被重用而引起的。

第二个问题是当checkboxes检查和滚动很少时,复选框值会更改,即,如果我检查图像视图的几个复选框并向下滚动以选择更多,checkboxes则检查另一个。有人可以帮我避免这个问题吗?

public class ImagesScreen extends Activity{

    public void onCreate(Bundle savedInstanceState){
       .......................

       .......................
          gridView.setAdapter(imagecursorAdapter);

    }

    }

适配器类:

public class ImageCursorAdapter extends SimpleCursorAdapter{

        private int layout;
        private LayoutInflater mLayoutInflater;
        private Context mContext;
        ViewHolder vh;

        public ImageAdapter(Context context, int layout, Cursor c,String[] from, int[] to)  {
            super(context, layout, c, from, to,0);
            this.layout = layout;
            mLayoutInflater=LayoutInflater.from(context);
            mContext = context;
        }

        public View newView(Context context, Cursor cursor, ViewGroup parent) {     
            View v = mLayoutInflater.inflate(layout, parent, false);
            return v;
        }

        public void bindView(final View v, final Context context, Cursor c) {
            final int id = c.getInt(c.getColumnIndex("id"));
            final String imagepath = c.getString(c.getColumnIndex("paths"));
            vh = new ViewHolder();

            vh.imageview = (ImageView)v.findViewById(R.id.imageView1);
            vh.checkBox = (CheckBox)v.findViewById(R.id.checkBox1);

            vh.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
       Log.d("ischecked",""+isChecked);                             
                }

            });
            File imgFile = new File(imagepath);
            if(imgFile.exists()){

                Bitmap imageBitmap = decodeFile(imgFile);
                vh.imageview.setImageBitmap(imageBitmap);
            }

        }

     static class ViewHolder{

            public ViewHolder(){}

            public ImageView imageview;
            public CheckBox checkBox;
        }
}
4

0 回答 0