Gridview和使用数组

时间:2009-12-29 18:49:27

标签: java android

首先,我是Android和此论坛的新手。我的问题是:我正在查看“Hello world view:gridview”示例,并且想知道,如何动态加载所有图像?例如,在ImageAdapter.java文件中,最后有:

private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

但是,如果我不知道图像名称(因为用户将添加新图像),该怎么办?我想要做的是从xml文件中获取图像名称,然后将其加载到一个数组中。我该如何做到这一点?

2 个答案:

答案 0 :(得分:0)

似乎您希望从静态资源以外的其他地方获取图像。 这里有一个链接,显示如何从远程位置检索图像。

http://www.anddev.org/gallery_with_remote_images-t769.html

答案 1 :(得分:0)

如果要动态显示设备上的图像,则应使用由游标适配器“支持”的Gridview(我建议使用SimpleCursorAdapter)。

您的适配器不是对数组中的图像名称进行硬编码,而是通过创建查询商店的“游标”来从orroids mediastore数据库获取图像名称。然后你的gridview可以通过'setAdapter()'调用来使用适配器。

以下是如何使用cursoradapter和gridview在图库中显示图像的示例:

http://android-er.blogspot.co.uk/2012/11/list-mediastoreimagesthumbnails-in.html

另外,您将需要在此stackoverflow答案中使用@achildress中提到的示例代码:Displaying images from a specific folder on the SDCard using a gridview

以下代码仅为您提供特定目录中的图像:

private Cursor cursor;
private int columnIndex;

首先,获取位于文件夹中的图像ID光标:

Gallery g = (Gallery) findViewById(R.id.gallery);
// request only the image ID to be returned
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection, 
        MediaStore.Images.Media.DATA + " like ? ",
        new String[] {"%myimagesfolder%"},  
        null);
// Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));

然后,在Gallery的ImageAdapter中,获取要显示的缩略图:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(context);
    // Move cursor to current position
    cursor.moveToPosition(position);
    // Get the current value for the requested column
    int imageID = cursor.getInt(columnIndex);
    // obtain the image URI
    Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
    String url = uri.toString();
    // Set the content of the image based on the image URI
    int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
    Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                    originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    i.setImageBitmap(b);
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);
    return i;
}