0

我正在寻找一些帮助来解决为什么 GridView 不显示正确数量的缩略图而是在组中显示大量重复的缩略图的问题?当我上下滚动缩略图列表并返回顶部时,图像的顺序一直在变化。

谁知道可能是什么原因以及如何解决?也许有更好的方法来创建缩略图的 GridView?还是我的手机?我使用的是带有 Android 版本 2.3.3 的 Samsung Young。我读到HTC也有类似的问题。

我真的很感激能够继续我的工作的一些帮助。谢谢!

下面是代码:

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

1 回答 1

0

将您的 getView 方法替换为以下内容:

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

告诉我它是否有效..

于 2013-02-17T12:19:05.653 回答