Filling a GridView with ImageViews Dynamically / Programmatically with ImageAdapter

I am trying to develop an Android application that allows the user to retrieve data from flickr and display it in a GridView (with some nice 3D animation). After some adventures, I almost ran, but now I'm stuck.


Here's the problem:

I got a user interface called "LoadPhotosTask" that receives images from flickr, just like the open source photostream application. In the onProgressUpdate(LoadedPhoto... value) method of this subclass, I call addPhoto() . So far, everything is in order - I have good Bitmap and Flickr.photo data with all the necessary information.

  @Override public void onProgressUpdate(LoadedPhoto... value) { addPhoto(value); } 

On the other hand, I have a GridView . Now I want to fill it with photos. It has an adapter called ImageAdapter (which extends BaseAdapter , see this tutorial ). If I use an array inside the ImageAdapter class, I can populate the GridView several samples. But if I want to fill it at runtime, I don't know what to do.

How do I configure the getView method in the ImageAdapter ? I tried to fill the array inside the ImageAdapter class ImageAdapter my values ​​in addPhoto , but it does not display anything.

So, first of all, I set up an array with the number of photos that I wanted to display in the grid (code 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 length of my photo sheet:

  final Flickr.PhotoList list = params[0]; final int count = list.getCount(); int helper = 0; imagead.setupArray(count); 

Subsequently, I manually call the getView method 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); } 

This is the getView method inside the ImageAdapter:

  public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { // if it 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; } 
+6
android dynamic adapter gridview flickr
source share
1 answer

You are missing the key part.

When you use the Adapter , you have the notifyDataSetChanged () method. Invalid logic:

When creating the Adapter for the GridView stay with the link 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 added should do the following:

 mPhotos.add(photo); mAdapter.notifyDataSetChanged(); 

What is it.

+9
source share

All Articles