2012年10月6日 星期六

Android GridView ImageAdapter

public class ImageAdapter extends BaseAdapter {
 
 private Context _context = null;
 private PhotoItem[] _photoArray = null;
 
 public ImageAdapter( Context ctx, PhotoItem[] imageArray )
 {
  _context = ctx;
  _photoArray = imageArray;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return _photoArray.length;
 }

 @Override
 public PhotoItem getItem(int pos) {
  // TODO Auto-generated method stub
  return _photoArray[ pos ];
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return 0;
 }

 @Override
 public View getView(int pos, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  View gridView;

  if ( convertView == null ) // if it's not recycled, initialize some attribute
  { 
   LayoutInflater vi = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   gridView = vi.inflate( R.layout.image_item, null);
   
   ImageView imageView = (ImageView)gridView.findViewById( R.id.imageItemView );
   imageView.setImageDrawable( _photoArray[ pos ].getPhoto() );
  }
  else
  {
   gridView = (View)convertView;
  }
 
  return gridView;
 }

}

http://www.mkyong.com/android/android-gridview-example/

沒有留言:

張貼留言