0

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;
        }

    }



}
erik
  • 4,946
  • 13
  • 70
  • 120
  • So, do you want two rows that scroll independently, or are you just creating a gallery that has two rows per item (getview call)? – dmon Apr 01 '12 at 22:12

1 Answers1

1

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().

dmon
  • 30,048
  • 8
  • 87
  • 96
  • i don't know what you are really saying.. i thought getCount returned the total number of images found.. what i am perplexed about is how to make getView() only get called every other image.. – erik Apr 01 '12 at 22:33
  • (I'm not talking about `cursor.getCount()`) So you want to display 2 things per get view right? Then your *adapter* needs to state that it has half of the things than it really has, that way, `getView()` will only get called half the times. Then, in `getView()` your duty is to render the two images that correspond to this position, and that's where the `pos*2` and `pos * 2 + 1` come in. – dmon Apr 01 '12 at 23:03
  • i think i get you.. so i changed my the adapters getCount method to return cursor.getCount()/2; right? and that works unless i have an od number of images.. so if i have 6 images.. AWESOME! it works right? but if i have 7 images.. well it stops after 6 and doesn't load the last one.. – erik Apr 02 '12 at 00:00
  • im updating my code above so you can see my adapter's get view and get count – erik Apr 02 '12 at 00:02
  • Yeah, I forgot to mention that you had to add an extra one for the odd ones :) . But you figured it out! – dmon Apr 02 '12 at 01:05
  • Step ahead of you on the floats and ceil.. thanks again for your help.. this was i think the simplest solution i have seen to the single row gallery problem – erik Apr 02 '12 at 01:23