ppamorim

Untitled

Jul 24th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. public class CameraGridAdapter implements SectionedAdapter {
  2.  
  3.     private Section section;
  4.     private List<Camera> mList;
  5.  
  6.     private Context mContext;
  7.     private OnGridOpenDetailsClick mCallbackDetails;
  8.  
  9.     private ViewHolder mViewHolder;
  10.  
  11.     public CameraGridAdapter(Context context, List<Camera> list,  OnGridOpenDetailsClick onOpenDetailsClick) {
  12.         mContext = context;
  13.         section = new Section();
  14.         mCallbackDetails = onOpenDetailsClick;
  15.         section.setSectionTitle("CameraGridAdapter");
  16.         mList = list;
  17.     }
  18.  
  19.     @Override
  20.     public View getHeaderViewForSection(int section, View convertView, ViewGroup parent) {
  21.         return null;
  22.     }
  23.  
  24.     @Override
  25.     public Class[] getViewTypes() {
  26.         return new Class[] { LinearLayout.class };
  27.     }
  28.  
  29.     @Override
  30.     public Class getViewType(FreeFlowItem proxy) {
  31.         return LinearLayout.class;
  32.     }
  33.  
  34.     @Override
  35.     public boolean shouldDisplaySectionHeaders() {
  36.         return false;
  37.     }
  38.  
  39.     public void update(ArrayList<Camera> camera){
  40.  
  41.         for(Object o : camera){
  42.             section.getData().add(o);
  43.         }
  44.  
  45.     }
  46.  
  47.     @Override
  48.     public long getItemId(int section, int position) {
  49.         return section * 1000 + position;
  50.     }
  51.  
  52.     @Override
  53.     public View getItemView(int section, int position, View convertView, ViewGroup parent) {
  54.  
  55.         mViewHolder = new ViewHolder();
  56.  
  57.         if(convertView == null) {
  58.  
  59.             convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_thumbnail_big, parent, false);
  60.             mViewHolder.background = (ImageView) convertView.findViewById(R.id.big_thumbnail);
  61.             mViewHolder.cityName = (TextView) convertView.findViewById(R.id.city);
  62.             mViewHolder.cameraName = (TextView) convertView.findViewById(R.id.camera_name);
  63.             convertView.setTag(mViewHolder);
  64.  
  65.         } else {
  66.             mViewHolder = (ViewHolder) convertView.getTag();
  67.         }
  68.  
  69.         Camera camera = getItem(position);
  70.  
  71.         mViewHolder.cityName.setText(camera.getCity() + " / " + camera.getStatePrefix());
  72.         mViewHolder.cameraName.setText(camera.getTitle());
  73.  
  74.         mViewHolder.background.setTag(position);
  75.         mViewHolder.cameraName.setTag(position);
  76.         mViewHolder.cityName.setTag(position);
  77.  
  78.         mViewHolder.background.setOnClickListener(onItemClick);
  79.  
  80.         ImageLoader.getInstance().displayImage(TagApplication.URL_TEMP_SERVER + camera.getUrlImage(), mViewHolder.background);
  81.  
  82.         return convertView;
  83.     }
  84.  
  85.     private View.OnClickListener onItemClick = new View.OnClickListener() {
  86.         @Override
  87.         public void onClick(View view) {
  88.  
  89.             Camera camera = getItem(Integer.valueOf(view.getTag().toString()));
  90.             if (camera != null && mCallbackDetails != null) {
  91.                 mCallbackDetails.onItemClick(camera);
  92.             }
  93.  
  94.         }
  95.     };
  96.  
  97.     //CRASH HERE
  98.  
  99.     public Camera getItem(int i) {
  100.  
  101.         Camera camera = new Camera();
  102.  
  103.         if(mList != null) {
  104.             return mList.get(i);
  105.         } else {
  106.             return camera;
  107.         }
  108.     }
  109.  
  110.     @Override
  111.     public int getNumberOfSections() {
  112.         if(section.getData().size() == 0) return 0;
  113.         return 1;
  114.     }
  115.  
  116.     @Override
  117.     public Section getSection(int index) {
  118.         return section;
  119.     }
  120.  
  121.     private static class ViewHolder {
  122.  
  123.         private ImageView background;
  124.         private TextView cameraName;
  125.         private TextView cityName;
  126.  
  127.     }
  128.  
  129.     public static interface OnGridOpenDetailsClick {
  130.         public void onItemClick(Camera camera);
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment