使用ImageAdapter动态/编程地填充GridView [英] Populating a GridView with ImageViews dynamically/programmatically using a ImageAdapter
问题描述
我尝试开发一个Android应用程序,允许用户从flickr中获取数据,并以 GridView
(使用一些不错的3D动画)显示。经过一些冒险,我得到了它几乎运行,但现在我被卡住了。
I try to develop an Android App which allows the user to fetch data from flickr and show it in a GridView
(with some nice 3D-Animation). After some adventures i got it almost running, but now I'm stuck.
这是问题:
我有一个UI线程LoadPhotosTask,它从flickr获取图片,就像开源应用程序 photostream 。在该子类的方法 onProgressUpdate(LoadedPhoto ... value)
中,我调用 addPhoto()
。直到现在,所有的东西都很好 - 我得到了一些不错的Bitmap和Flickr.photo数据,我需要所有的信息。
I got a UI Thread "LoadPhotosTask" which gets the pictures from flickr, just like the open source application photostream. In the method onProgressUpdate(LoadedPhoto... value)
of that subclass I call addPhoto()
. Until now everythings fine - I got some nice Bitmap and Flickr.photo data with all the information I need.
@Override
public void onProgressUpdate(LoadedPhoto... value) {
addPhoto(value);
}
另一方面,我有一个 GridView
。现在我想填写照片。它有一个名为 ImageAdapter
(扩展 BaseAdapter
的适配器,请参阅这个教程)。如果我在 ImageAdapter
类中使用数组,我可以使用一些示例图像填充 GridView
。但是如果我想在运行时填充它,我不知道该怎么做。
On the other hand I have got a GridView
. Now I want to fill it with the Photos. It has got an adapter called ImageAdapter
(which extends BaseAdapter
, see this tutorial). If I use an array inside the ImageAdapter
class I can populate the GridView
with some sample images. But if I want to populate it at runtime, I don't know what to do.
如何在$ $ c $中设置getView方法C> ImageAdapter ?我试图用 addPhoto
中的值填充 ImageAdapter
类中的数组,但不显示任何内容。
How do I have to set up the getView method in the ImageAdapter
? I was trying to fill the array inside the ImageAdapter
class with my values in addPhoto
, but it doesn't display anything.
所以首先我正在设置数组,我想在网格中显示的照片数量(代码位于 ImageAdapter
class):
So first of all I was setting up the array with the amount of Photos i wanted to display in the grid like that (code is inside the ImageAdapter
class):
// class variable
private ImageView[] mThumbIds;
[...]
public void setupArray(int count) {
this.mThumbIds = new ImageView[count];
}
然后我用我的照片列表的长度调用这个方法:
Then I call this method with the lenght of my photolist:
final Flickr.PhotoList list = params[0];
final int count = list.getCount();
int helper = 0;
imagead.setupArray(count);
之后我在addPhoto方法中手动调用getView方法:
Afterwards I call the getView method manually inside the addPhoto method:
private void addPhoto(LoadedPhoto... value) {
ImageView image = (ImageView) mInflater.inflate(
R.layout.grid_item_photo, null);
image.setImageBitmap(value[0].mBitmap);
image.setTag(value[0].mPhoto);
imagead.setmThumbIds(image, value[0].mPosition);
imagead.getView(value[0].mPosition, null, mpicturesGrid);
}
这是ImageAdapter中的getView方法:
That is the getView method inside ImageAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(EDGE_LENGTH,
EDGE_LENGTH));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
imageView.setVisibility(View.VISIBLE);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(mThumbIds[position].getDrawable());
imageView.setTag(mThumbIds[position].getTag());
return imageView;
}
推荐答案
缺少关键部分。
当您使用适配器
时,您有一种方法称为 notifyDataSetChanged()。
您缺少的逻辑如下:
When you use an Adapter
you have a method called notifyDataSetChanged().
The logic you are missing there is the following:
在适配器
中为 GridView
保留适配器将使用的列表的引用。如下所示:
When creating the Adapter
for the GridView
stay with a reference for the list that the adapter will use. Something like:
private ArrayList<Photo> mPhotos;
private BaseAdapter mAdapter;
private GridView mGridView;
onCreate:
/* other things here */
mAdapter = new MyAdapter(mPhotos);
mGridView.setAdapter(mAdapter);
您添加的照片应该如下:
What you addPhoto should do is the following:
mPhotos.add(photo);
mAdapter.notifyDataSetChanged();
就是这样。
这篇关于使用ImageAdapter动态/编程地填充GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!