填充一个GridView与ImageViews动态/编程使用ImageAdapter [英] Populating a GridView with ImageViews dynamically/programmatically using a ImageAdapter
问题描述
我尝试开发一个Android应用程序,它允许用户通过Flickr获取数据并显示在一个的GridView
(有一些不错的3D动画)。有些冒险后,我得到它几乎是跑着,但现在我卡住了。
下面的问题:
我有一个UI线程LoadPhotosTask它获取图片来自Flickr,就像开源应用程序<一href="http://$c$c.google.com/p/apps-for-android/source/browse/trunk/#trunk/Photostream">photostream.该方法 onProgressUpdate(LoadedPhoto ...值)
的子类,我叫了 addPhoto()
。到现在为止万物精 - 我得到了一些不错的位图和Flickr.photo数据与所有我需要的信息
@覆盖
公共无效onProgressUpdate(LoadedPhoto ...值){
addPhoto(值);
}
在另一方面,我有我的的GridView
。现在我想用照片填充它。它得到了 ImageAdapter
称为适配器(延伸 BaseAdapter
,看到这个的教程)。如果我使用 ImageAdapter
类中的数组我可以填充的GridView
有一些示例图像。但是,如果我想在运行时填充它,我不知道该怎么办。
我如何必须建立在getView方法中的 ImageAdapter
?我试图填补了 ImageAdapter
与我的价值观类 addPhoto
,但它不显示任何内部数组
所以首先我设置了照片的数量的数组我想在这样的网格显示(code是 ImageAdapter
类中):
//类变量
私人的ImageView [] mThumbIds;
[...]
公共无效setupArray(诠释计数){
this.mThumbIds =新的ImageView [统计]
}
然后我把这种方法,我photolist的长度:
最后Flickr.PhotoList列表= PARAMS [0];
最终诠释计数= list.getCount();
INT帮手= 0;
imagead.setupArray(计数);
后来我手动调用getView方法addPhoto方法中:
私人无效addPhoto(LoadedPhoto ...值){
ImageView的形象=(ImageView的)mInflater.inflate(
R.layout.grid_item_photo,NULL);
image.setImageBitmap(值[0] .mBitmap);
image.setTag(值[0] .mPhoto);
imagead.setmThumbIds(图像,值[0] .mPosition);
imagead.getView(值[0] .mPosition,空,mpicturesGrid);
}
这是getView方法中ImageAdapter:
公开查看getView(INT位置,查看convertView,ViewGroup中父){
如果(convertView == NULL){//如果它不回收,初始化一些
//属性
ImageView的=新ImageView的(mContext);
imageView.setLayoutParams(新GridView.LayoutParams(EDGE_LENGTH,
EDGE_LENGTH));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0,0,0,0);
imageView.setVisibility(View.VISIBLE);
} 其他 {
ImageView的=(ImageView的)convertView;
}
imageView.setImageDrawable(mThumbIds [位置] .getDrawable());
imageView.setTag(mThumbIds [位置] .getTag());
返回ImageView的;
}
您缺少的重要组成部分。
当您使用适配器
你有一个方法叫 notifyDataSetChanged()。
你缺少存在的逻辑如下:
在创建适配器
为的GridView
留在该适配器将使用该列表的参考。是这样的:
私人的ArrayList&LT;图片&GT; mPhotos;
私人BaseAdapter mAdapter;
私人的GridView mGridView;
的onCreate:
/ *这里其他的东西* /
mAdapter =新MyAdapter(mPhotos);
mGridView.setAdapter(mAdapter);
什么,你addPhoto应该做的是以下几点:
mPhotos.add(照片);
mAdapter.notifyDataSetChanged();
就是这样。
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;
}
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 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();
That's it.
这篇关于填充一个GridView与ImageViews动态/编程使用ImageAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!