GridView和使用数组 [英] Gridview and using array
问题描述
首先,我是新来的Android和这个论坛发展。我的问题是这样的:我在看世界,你好观点:GridView控件的例子,在想,我怎么动态装载所有我的图片?例如,在 ImageAdapter.java
文件,在年底有:
First, I'm new to developing for Android and this forum. My question is this: I was looking at the "Hello world view: gridview" example and was wondering, how do I load all my images dynamically? For example in the ImageAdapter.java
file, at the end there is:
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文件抓取图像名称,然后将其加载到一个数组。我如何做到这一点?
But what if I don't know the images name (as the user will add new images). What I would like to do is grab the image name from an xml file and then load it into an array. How do I accomplish this?
推荐答案
如果要动态显示,是你的设备,你应该光标适配器使用GridView'支持'上的图像(我建议一个SimpleCursorAdapter)。
If you want to dynamically show images that are on your device, you should use a Gridview 'backed' by a cursor adaptor (I would suggest a SimpleCursorAdapter).
相反数组中的硬编码图像的名字,你的适配器将通过创建光标,查询商店获得机器人mediastore数据库中的图像名称。然后你的GridView可以通过执行setAdapter()调用中使用的适配器。
Instead of hardcoding the image names in an array, your adapter will get the image names from androids mediastore database by creating a 'cursor' that queries the store. Then your gridview can use the adapter by doing a 'setAdapter()' call.
下面是如何使用的CursorAdapter和GridView显示在您的画廊图像的例子:
Here is an example of how to display the images in your gallery using a cursoradapter and gridview:
的http://android-er.blogspot.co.uk/2012/11/list-mediastoreimagesthumbnails-in.html
另外,你将要使用这个计算器的答案@achildress答案提到的示例code:<一href=\"http://stackoverflow.com/questions/5039779/displaying-images-from-a-specific-folder-on-the-sdcard-using-a-gridview\">Displaying使用一个GridView 从SD卡中的特定文件夹的图像
Plus, you will want to use the sample code mentioned in @achildress answer in this stackoverflow answer: Displaying images from a specific folder on the SDCard using a gridview
以下code会给你在特定目录仅适用于图像:
The following code will give you only images in your specific directory:
private Cursor cursor;
private int columnIndex;
首先,获得所在的文件夹中的图像ID的光标:
First, obtain a cursor of image IDs located in the folder:
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));
然后,在ImageAdapter的画廊,取得缩略图显示:
Then, in the ImageAdapter for the Gallery, obtain the thumbnail to display:
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;
}
这篇关于GridView和使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!