使用 ImageAdapter 以动态/编程方式使用 ImageView 填充 GridView
我尝试开发一个 Android 应用程序,它允许用户从 flickr 获取数据并将其显示在 GridView 中(带有一些漂亮的 3D 动画)。经过一番冒险之后,我几乎可以运行了,但现在我陷入了困境。
问题是:
我有一个 UI 线程“LoadPhotosTask”,它从 flickr 获取图片,就像开源应用程序 照片流。在该子类的方法 onProgressUpdate(LoadedPhoto... value)
中,我调用 addPhoto()
。到目前为止,一切都很好 - 我得到了一些不错的位图和 Flickr.photo 数据,其中包含我需要的所有信息。
@Override
public void onProgressUpdate(LoadedPhoto... value) {
addPhoto(value);
}
另一方面,我有一个 GridView。现在我想用照片填充它。它有一个名为 ImageAdapter
的适配器(它扩展了 BaseAdapter
,请参阅此 教程)。如果我在 ImageAdapter 类中使用数组,我可以使用一些示例图像填充 GridView。但如果我想在运行时填充它,我不知道该怎么做。
我如何在 ImageAdapter
中设置 getView 方法?我试图用 addPhoto
中的值填充 ImageAdapter
类内的数组,但它不显示任何内容。
所以首先我用我想要在网格中显示的照片数量设置数组(代码位于 ImageAdapter 类中):
// class variable
private ImageView[] mThumbIds;
[...]
public void setupArray(int count) {
this.mThumbIds = new ImageView[count];
}
然后我用我的长度调用此方法photolist:
final Flickr.PhotoList list = params[0];
final int count = list.getCount();
int helper = 0;
imagead.setupArray(count);
然后我在addPhoto方法中手动调用getView方法:
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方法:
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;
}
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.
Here's the problem:
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);
}
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.
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.
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);
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);
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你缺少一个关键部分。
当您使用
Adapter
时,您有一个名为 notifyDataSetChanged()。您缺少的逻辑如下:
为 GridView 创建 Adapter 时,保留适配器将使用的列表的引用。类似于:
您 addPhoto 应该执行以下操作:
就是这样。
You are missing a key part.
When you use an
Adapter
you have a method called notifyDataSetChanged().The logic you are missing there is the following:
When creating the
Adapter
for theGridView
stay with a reference for the list that the adapter will use. Something like:What you addPhoto should do is the following:
That's it.