Java Gridview与数组的使用
首先,我对Android和这个论坛的开发还不熟悉。我的问题是:我在看Hello world view:gridview示例,想知道如何动态加载所有图像?例如,在ImageAdapter.java文件的末尾有:Java Gridview与数组的使用,java,android,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.dra
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文件中获取图像名称,然后将其加载到数组中。如何实现这一点?您似乎希望从静态资源以外的其他地方获取图像。 这里有一个链接显示了如何从远程位置检索图像
如果您想动态显示设备上的图像,您应该使用由光标适配器“支持”的Gridview,我建议使用SimpleCursorAdapter 您的适配器将通过创建查询存储的“游标”从androids mediastore数据库获取图像名称,而不是在数组中硬编码图像名称。然后,gridview可以通过执行“setAdapter”调用来使用适配器 以下是如何使用光标适配器和gridview在图库中显示图像的示例: 此外,您还需要在stackoverflow答案中使用@achildress answer中提到的示例代码: 以下代码将仅提供特定目录中的图像:
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));
然后,在库的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;
}
我有点困惑。您是在问如何从XML文件中获取图像名称,还是在问一旦有了图像名称数组,如何获取图像?或者你是在问如何将这些图像放在gridview中?很抱歉造成混淆。我想把图像放到一个网格视图中,但是因为我不一定知道图像的名称,所以我需要以动态的方式添加它们。图像将存储在手机上,而不是通过互联网下载。我想做的是:1。在drawable文件夹中,我有一组图像。我不知道它们的名字,因为它们将由用户在运行时2添加。我想把这些图像加载到GridView中,你明白了吗?我也想知道同样的事情,但我不懂java,我是一个php开发人员。在我的例子中,文件名为c1-35.png,所以在php中我可以创建一个for循环并添加它们,但我不确定它如何与java和嵌套类drawable一起工作。不,图像是一个静态资源,用户可以将图像保存到文件夹中,我希望gridview显示该文件夹中的所有图像