Guest User

Fragment with GridView

a guest
Jul 19th, 2011
3,575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package dsndata.sds2mobile.ontrack.fragments;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.Vector;
  6.  
  7. import dsndata.sds2mobile.ontrack.activities.R;
  8. import dsndata.sds2mobile.ontrack.classes.ListMetaData;
  9. import dsndata.sds2mobile.ontrack.classes.Row;
  10. import android.app.Fragment;
  11. import android.content.Context;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.ArrayAdapter;
  18. import android.widget.BaseAdapter;
  19. import android.widget.GridView;
  20. import android.widget.TextView;
  21.  
  22. public class BulkScanChosenAttributes extends Fragment
  23. {
  24.     private static final String TAG = "BulkScanChosenAttributes";
  25.     private ArrayList<Row> items;
  26.     private GridAttributeAdapter adapter;
  27.    
  28.     public BulkScanChosenAttributes()
  29.     {
  30.         items = null;
  31.         adapter = null;
  32.     }
  33.    
  34.     public BulkScanChosenAttributes(ArrayList<Row> items)
  35.     {
  36.         this.items = items;
  37.         adapter = null;
  38.     }
  39.    
  40.     @Override
  41.     public void onActivityCreated(Bundle savedInstance)
  42.     {
  43.         super.onActivityCreated(savedInstance);
  44.        
  45. //      adapter = new GridAttributeAdapter(getActivity().getApplicationContext(), items);
  46. //     
  47. //      LayoutInflater gridInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  48. //      View v = gridInflater.inflate(R.layout.bulk_scan_chosen, null);
  49. //      gridView = (GridView) v.findViewById(R.id.gridview);
  50. //     
  51. //      gridView.setAdapter(adapter);
  52.        
  53.     }
  54.    
  55.     @Override
  56.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  57.     {
  58.         return inflater.inflate(R.layout.bulk_scan_chosen, container, false);
  59.     }
  60.    
  61.     @Override
  62.     public void onCreate(Bundle savedInstance)
  63.     {
  64.         super.onCreate(savedInstance);
  65.        
  66.         adapter = new GridAttributeAdapter(getActivity().getApplicationContext(), R.layout.bulk_scan_chosen, items);
  67.     }
  68.    
  69.     @Override
  70.     public void onStart()
  71.     {
  72.         super.onStart();
  73.        
  74.         GridView v = (GridView) getView();
  75.        
  76.         v.setAdapter(adapter); 
  77.     }
  78.    
  79.     public class GridAttributeAdapter extends ArrayAdapter<Row>
  80.     {
  81.         private int count;
  82.         private LayoutInflater inflater;
  83.         private Context context;
  84.         private ArrayList<Row> items;
  85.        
  86.         public GridAttributeAdapter(Context context, int resource, ArrayList<Row> items)
  87.         {
  88.             super(context, resource);
  89.             int count = 0;
  90.             this.context = context;
  91.             this.items = items;
  92.             inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  93.         }
  94.         @Override
  95.         public int getCount()
  96.         {
  97.             return count;
  98.         }
  99.  
  100.         @Override
  101.         public Row getItem(int position)
  102.         {
  103.             return items.get(position);
  104.         }
  105.  
  106.         @Override
  107.         public long getItemId(int position)
  108.         {
  109.             return position;
  110.         }
  111.        
  112.         public void setItems(ArrayList<Row> items)
  113.         {
  114.             this.items = items;
  115.         }
  116.  
  117.         @Override
  118.         public View getView(int position, View convertView, ViewGroup parent)
  119.         {
  120.             Log.d(TAG, "calling getView in the grid adapter");
  121.             View v = convertView;
  122.             //try to inflate the view
  123.             try
  124.             {
  125.                 v = inflater.inflate(R.layout.grid_item_layout, null);
  126.             }
  127.             catch (NullPointerException npe)
  128.             {
  129.                 npe.printStackTrace();
  130.                 Log.e(TAG, "view is null");
  131.             }
  132.            
  133.             Row row = items.get(position);
  134.            
  135.             try
  136.             {
  137.                 TextView name = (TextView) v.findViewById(R.id.grid_att_name);
  138.                 TextView value = (TextView) v.findViewById(R.id.grid_att_value);
  139.                
  140.                 if (ListMetaData.withDates.contains(ListMetaData.reverseScreenNames.get(row.getRowName())))
  141.                 {
  142.                     try
  143.                     {
  144.                         Date date = new Date(new Long(row.getRowValue()));
  145.                         value.setText(date.getMonth() + "/" + date.getDate() + "/" + date.getYear());
  146.                     }
  147.                     catch (NumberFormatException nfe)
  148.                     {
  149.                         value.setText(row.getRowValue());
  150.                     }
  151.                 }
  152.                 else
  153.                 {
  154.                     value.setText(row.getRowValue());
  155.                 }
  156.                    
  157.                 name.setText(row.getRowName());
  158.             }
  159.             catch (NullPointerException npe)
  160.             {
  161.                 npe.printStackTrace();
  162.                 Log.e(TAG, "something went wrong in TextView assignment");
  163.             }
  164.            
  165.             return v;
  166.         }
  167.        
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment