Search code examples
androidandroid-galleryandroid-adapter

Gallery and baseAdapter incrementation android


So, i like many have been battling to create a gallery that has two rows for android..

so far my solution is to have a base adapter call lay out that has two table rows with a spot for a photo in each, and then in my base adapter place one image then move my cursor to the next position and place another image in the second place holder.. it is working so far accept that we end up with repeated images.. so i place image1 and image 2 in the first pass and then the next pass it places image2 over image3,.. is there a way to set the increment to which my base adapter is called?

FIXED CODE BELOW

public class GallAdapter extends BaseAdapter {
        public Cursor cursor;
        private int columnIndex;
        private Context context;
        int imageBackground;
        String url;
        Uri uri;
        int originalImageId;
        int imageID;
        int columnData;
        ViewGroup myp;
        ImageView d;
        ImageView d2;
        Boolean done = false;

        public GallAdapter(Context ctx, Cursor cur, int cIn ) {
            context = ctx;
            columnIndex = cIn;

            cursor = cur;

        }

        @Override
        public int getCount() {
            Toast.makeText(context, "old:"+cursor.getCount()+" 1/2:"+cursor.getCount()/2+" roundedup:"+Math.ceil((float)cursor.getCount()/2), Toast.LENGTH_SHORT).show();
            return (int) Math.ceil((float)cursor.getCount()/2);
        }

        @Override
        public Object getItem(int position) {

            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            myp = parent;
            View v;

            if(convertView == null){
                v = LayoutInflater.from(context).inflate(R.layout.galitem2, parent, false);
            }else{
                v = convertView;

            }


            ImageView photo = (ImageView) v.findViewById(R.id.imageView);
            ImageView border = (ImageView) v.findViewById(R.id.borderView);
            d = (ImageView) v.findViewById(R.id.delView);

            ImageView photo2 = (ImageView) v.findViewById(R.id.image2View);
            ImageView border2 = (ImageView) v.findViewById(R.id.border2View);
            d2 = (ImageView) v.findViewById(R.id.del2View);

                cursor.moveToPosition(position*2);

                // Get the current value for the requested column
                imageID = cursor.getInt(columnIndex);
                // obtain the image URI
                uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                url = uri.toString();
                // Set the content of the image based on the image URI
                originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                photo.setImageBitmap(b);

                photo.setScaleType(ImageView.ScaleType.FIT_CENTER); 
                d.setTag(uri);
                d.setOnClickListener(new OnClickListener(){


                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        String path = getRealPathFromURI((Uri) v.getTag());

                        File file = new File(path);
                        file.delete();
                        getContentResolver().delete((Uri) v.getTag(), null, null);
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/LC/images/")));
                        Intent galView = new Intent(getBaseContext(), GalleryView2.class);
                        galView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        getBaseContext().startActivity(galView);
                        finish();


                    }


                });
               if(position*2+1<=cursor.getCount()-1){
                  // Move cursor NEXT current position

                    cursor.moveToPosition(position*2+1);

                    // Get the current value for the requested column
                    imageID = cursor.getInt(columnIndex);
                    // obtain the image URI
                    uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                    url = uri.toString();
                    // Set the content of the image based on the image URI
                    originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                    Bitmap b2 = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
                                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                    photo2.setImageBitmap(b2);

                    photo2.setScaleType(ImageView.ScaleType.FIT_CENTER);    
                    d2.setTag(uri);
                    d2.setOnClickListener(new OnClickListener(){


                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            String path = getRealPathFromURI((Uri) v.getTag());

                            File file = new File(path);
                            file.delete();
                            getContentResolver().delete((Uri) v.getTag(), null, null);
                            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/LC/images/")));
                            Intent galView = new Intent(getBaseContext(), GalleryView2.class);
                            galView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getBaseContext().startActivity(galView);
                            finish();


                        }


                    });
                }else{
                    //border2.setVisibility(v.INVISIBLE);
                    d2.setVisibility(v.INVISIBLE);

               }

            return v;
        }

    }



}

Solution

  • Wait, isn't what you want just:

    first photo -> cursor.moveToPosition(position * 2);
    second photo -> cursor.moveToPosition(position * 2 + 1);
    

    You just need to return the right size in your adapter's getCount().