0

I'm looking for some help to solve the problem why the GridView don't show the correct number of thumbnails and instead shows a lot of duplicates of the thumbnails in groups? And when I scroll the list of thumbnails up en down and return to the top, the order of the images changes all the time.

Anyone who knows what the reason could be and how to solve it? Perhaps there is a better way to create a GridView of thumbnails? Or is it my phone? I use a Samsung Young with Android version 2.3.3. I read that the HTC had a similar problem.

I would really preciate some help to be able to continue my work. Thanks!

Below is the code:

public class MainActivity extends Activity {

Cursor cursor;
int indexColumn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Array for the columns to access
    String[] projection = {MediaStore.Images.Thumbnails._ID};

    // Cursor object
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);

    // Number of columns
    indexColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this)); // Send this class to constructor

    /*
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
    */


    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });   
}


// Nested class
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Constructor
    public ImageAdapter(Context c) {
        mContext = c;   
    }

    public int getCount() {
        return cursor.getCount();
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);

            // Move cursor to current position
            cursor.moveToPosition(position);

            // Get the current value value for the requested column
            int imageId = cursor.getInt(indexColumn);

            // Set content of the image
            imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageId));

            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);


        } else {
            imageView = (ImageView) convertView;
        }

        //imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
}   
}
3
  • Surprised that not anyone has any ideas how to solve the problems!?
    – 3D-kreativ
    Feb 17, 2013 at 11:25
  • Hello! Anyone out there that have some ideas what could be wrong? Preciate some help!
    – 3D-kreativ
    Feb 18, 2013 at 9:04
  • It seems that this problem only happens when I run the application om may Samsung Young with version 2.3.3 of Android. When I test it on the emulator it works fine! This is odd, because I run the emulator with the same version of Android. Why this? Anyone that have any ideas? Is there a alternative way to create a GridView of thumbnails?
    – 3D-kreativ
    Feb 18, 2013 at 11:15

1 Answer 1

0

Replace your getView method with the following:

// create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);

        } else {
            imageView = (ImageView) convertView;
        }

        // Move cursor to current position
            cursor.moveToPosition(position);

            // Get the current value value for the requested column
            int imageId = cursor.getInt(indexColumn);
            // Set content of the image
                  imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageId));
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        return imageView;
    }

Tell me if it works..

3
  • Thanks for the reply! Sorry but it's not working! One thing is better and that is that the GridView shows the correct number om images, but you can still scroll the list a very long way down. And when I return back to the GridView after having the image displayed in full screen in another activity the app crash when I scroll the list! Any other ideas how to solve this?
    – 3D-kreativ
    Feb 17, 2013 at 12:31
  • How do I do that? Can I add a text file here?
    – 3D-kreativ
    Feb 17, 2013 at 12:50
  • I removed the LogCat here since it was to long
    – 3D-kreativ
    Feb 18, 2013 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.