Advertisement
moonlightcheese

cursors

Jul 21st, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.29 KB | None | 0 0
  1.     /** inner classes **/
  2.     public class ShipKitAdapter extends CursorAdapter {
  3.        
  4.         public ShipKitAdapter(Context context, Cursor cur) {
  5.             super(context, cur, true);
  6.         }
  7.        
  8.         public void bindView(View view, Context context, Cursor cursor) {
  9.             try {
  10.                 ((TextView)view.findViewById(R.id.kit_id)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_ID)));
  11.                 ((TextView)view.findViewById(R.id.net_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_NET)));
  12.                 ((TextView)view.findViewById(R.id.net_uom)).setText("lbs");
  13.                 ((TextView)view.findViewById(R.id.product_name)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.ProductSchema.TABLE_NAME+DbSchema.ProductSchema.COLUMN_NAME)));
  14.                 ((TextView)view.findViewById(R.id.gross_units)).setText(cursor.getString(cursor.getColumnIndex(DbSchema.KitSchema.TABLE_NAME+DbSchema.KitSchema.COLUMN_GROSS)));
  15.                 ((TextView)view.findViewById(R.id.gross_uom)).setText("lbs");
  16.             } catch(Exception e) {
  17.                 Log.i(LOG_TAG, "error getting a field: "+e.getMessage());
  18.             }
  19.         }
  20.        
  21.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  22.             return mInflater.inflate(R.layout.ship_list_item, parent, false);
  23.         }
  24.     };
  25.  
  26.  
  27. ////////////////////////////////////////////////////////////
  28.  
  29. class DashboardCursorAdapter extends SimpleCursorAdapter {
  30.         int[] mTo;
  31.         String[] mFrom;
  32.         Context mCtx;
  33.         LayoutInflater mInflater;
  34.        
  35.         public DashboardCursorAdapter(Context ctx, int layout, Cursor cur, String[] from, int[] to) {
  36.             super(ctx, layout, cur, from, to);
  37.             mFrom = from;
  38.             mTo = to;
  39.             mCtx = ctx;
  40.             mInflater = LayoutInflater.from(ctx);
  41.         }
  42.        
  43.         @Override
  44.         public View getView(int position, View view, ViewGroup parent) {
  45.             view = mInflater.inflate(R.layout.list_view_item_2, null);
  46.             SharedPreferences prefs = mCtx.getSharedPreferences("dashboardprefs", 0);
  47.             Cursor cursor = getCursor();
  48.             cursor.moveToPosition(position);
  49.            
  50.             String productText = new String("");
  51.             String uomText = new String("");
  52.             String totalText = new String("");
  53.             String unitsText = new String("");
  54.             String acogText = new String("");
  55.             int tempCount = 0;
  56.            
  57.             try {
  58.                 productText = cursor.getString(cursor.getColumnIndex(mFrom[tempCount])); tempCount++;
  59.                 uomText = cursor.getString(cursor.getColumnIndex(mFrom[tempCount])); tempCount++;
  60.                 totalText = cursor.getString(cursor.getColumnIndex(mFrom[tempCount])); tempCount++;
  61.                 unitsText = cursor.getString(cursor.getColumnIndex(mFrom[tempCount])); tempCount++;
  62.                 acogText = cursor.getString(cursor.getColumnIndex(mFrom[tempCount]));
  63.             } catch(Exception e) {
  64.                 Log.d(LOG_TAG, "Exception retrieving: ");
  65.                 String[] colNames = cursor.getColumnNames();
  66.                 for(int i=0; i<colNames.length; i++) {
  67.                     Log.d(LOG_TAG, "Column: "+colNames[i]);
  68.                 }
  69.             }
  70.            
  71.             TextView product = (TextView)view.findViewById(R.id.item_text_product);
  72.             TextView uom = (TextView)view.findViewById(R.id.item_text_uom);
  73.             TextView total = (TextView)view.findViewById(R.id.item_text_total);
  74.             TextView units = (TextView)view.findViewById(R.id.item_text_units);
  75.             TextView acog = (TextView)view.findViewById(R.id.item_text_acog);
  76.             TextView acogQualifier = (TextView)view.findViewById(R.id.text_acog);
  77.            
  78.             if(prefs.getInt("color_mode", ColorActivity.LAYOUT_BRIGHT) == ColorActivity.LAYOUT_BRIGHT) {
  79.                 view.setBackgroundColor(prefs.getInt("bg_bright", Color.YELLOW));
  80.                 product.setTextColor(prefs.getInt("product_bright", Color.DKGRAY));
  81.                 uom.setTextColor(prefs.getInt("units_bright", Color.DKGRAY));
  82.                 total.setTextColor(prefs.getInt("total_bright", Color.DKGRAY));
  83.                 units.setTextColor(prefs.getInt("units_bright", Color.DKGRAY));
  84.                 acog.setTextColor(prefs.getInt("cost_bright", Color.DKGRAY));
  85.                 acogQualifier.setTextColor(prefs.getInt("cost_bright", Color.DKGRAY));
  86.             } else {
  87.                 view.setBackgroundColor(prefs.getInt("bg_normal", Color.BLACK));
  88.                 product.setTextColor(prefs.getInt("product_normal", Color.LTGRAY));
  89.                 uom.setTextColor(prefs.getInt("units_normal", Color.LTGRAY));
  90.                 total.setTextColor(prefs.getInt("total_normal", Color.LTGRAY));
  91.                 units.setTextColor(prefs.getInt("units_normal", Color.LTGRAY));
  92.                 acog.setTextColor(prefs.getInt("cost_normal", Color.LTGRAY));
  93.                 acogQualifier.setTextColor(prefs.getInt("cost_normal", Color.LTGRAY));
  94.             }
  95.             product.setText(productText);
  96.             uom.setText(uomText);
  97.             total.setText(totalText);
  98.             units.setText(unitsText);
  99.             acog.setText(acogText);
  100.             return view;
  101.         }
  102.     };
  103.  
  104.  
  105. /////////////////////////////////////////////////
  106.  
  107. class CustomExpandableListAdapter extends SimpleExpandableListAdapter {
  108.         Context mCtx;
  109.         LayoutInflater vi;
  110.         public List<List<Boolean>> checkData;
  111.         String[] mChildFrom;
  112.         int[] mChildTo;
  113.        
  114.         public CustomExpandableListAdapter(Context ctx, List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>>childData, int childLayout, String[] childFrom, int[] childTo, List<List<Boolean>> checkData) {
  115.             super(ctx, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
  116.             this.mCtx=ctx;
  117.             this.vi = (LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  118.             this.checkData = checkData;
  119.             this.mChildFrom = childFrom;
  120.             this.mChildTo = childTo;
  121.         }
  122.        
  123.         public boolean updateChecked(List<List<Boolean>> newData) {
  124.             checkData = newData;
  125.             return true;
  126.         }
  127.        
  128.         public boolean isChecked(int groupPosition, int childPosition) {
  129.             return checkData.get(groupPosition).get(childPosition);
  130.         }
  131.        
  132.         private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
  133.             int len = to.length;
  134.            
  135.             for (int i = 0; i < len; i++) {
  136.                 TextView v = (TextView)view.findViewById(to[i]);
  137.                 if (v != null) {
  138.                     v.setText((String)data.get(from[i]));
  139.                 }
  140.             }
  141.         }
  142.        
  143.         public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  144.             View rowView;
  145.             rowView = new RelativeLayout(mCtx);
  146.             if(vi != null) {
  147.                 vi.inflate(R.layout.call_list_item, (RelativeLayout)rowView, true);
  148.             }
  149.            
  150.             if(this.checkData.get(groupPosition).get(childPosition)) {
  151.                 ((CheckedTextView)rowView.findViewById(R.id.checkmark)).setChecked(true);
  152.             } else {
  153.                 ((CheckedTextView)rowView.findViewById(R.id.checkmark)).setChecked(false);
  154.             }
  155.            
  156.             switch(mMode) {
  157.                 case MODE_PENDING:
  158.                     if(((TextView)rowView.findViewById(R.id.item_text_job))!=null){
  159.                         ((TextView)rowView.findViewById(R.id.item_text_job)).setVisibility(View.VISIBLE);
  160.                     }
  161.                     break;
  162.                 case MODE_BILLING:
  163.                     //rowView.forceLayout();
  164.                     break;
  165.                 default:
  166.                     break;
  167.             }
  168.            
  169.             //if the child is in the "Not Registered" group, display different fields
  170.             String display_name = null;
  171.            
  172.             if(groupData.get(groupPosition).containsValue("Not Registered")) {
  173.                 display_name = childData.get(groupPosition).get(childPosition).get("display_name");
  174.                
  175.                 if(display_name != null && !display_name.equals("")) {
  176.                     TextView displayNameView = ((TextView)rowView.findViewById(R.id.text_minutes_billed));
  177.                     RelativeLayout.LayoutParams displayNameParams = ((RelativeLayout.LayoutParams)(displayNameView.getLayoutParams()));
  178.                     displayNameParams.addRule(RelativeLayout.BELOW, R.id.item_hidden_number);
  179.                     displayNameView.setLayoutParams(displayNameParams);
  180.                     displayNameView.setText(display_name);
  181.                 }
  182.             }
  183.            
  184.             bindView(rowView, childData.get(groupPosition).get(childPosition), mChildFrom, mChildTo);
  185.             if(groupData.get(groupPosition).containsValue("Not Registered")) {
  186.                 if(display_name!=null && !display_name.equals("")) {
  187.                     ((TextView)rowView.findViewById(R.id.text_minutes_billed)).setText(display_name);
  188.                     ((TextView)rowView.findViewById(R.id.text_minutes_billed)).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
  189.                 } else {
  190.                     ((TextView)rowView.findViewById(R.id.text_minutes_billed)).setVisibility(View.GONE);
  191.                 }
  192.                 ((TextView)rowView.findViewById(R.id.item_text_duration_billed)).setVisibility(View.GONE);
  193.             }
  194.             //return super.getChildView(groupPosition, childPosition, isLastChild, rowView, parent);
  195.             return rowView;
  196.         }
  197.        
  198.         public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  199.             RelativeLayout rowView;
  200.             if(convertView == null) {
  201.                 rowView = new RelativeLayout(mCtx);
  202.                 if(vi!=null){
  203.                     vi.inflate(R.layout.call_group_item, rowView, true);
  204.                 }
  205.             } else {
  206.                 rowView = (RelativeLayout)convertView;
  207.             }
  208.            
  209.             if(!(((TextView)rowView.findViewById(R.id.item_text_job)) == null) )
  210.             {
  211.                 ((TextView)rowView.findViewById(R.id.item_text_job)).setVisibility(View.GONE);
  212.             }
  213.            
  214.             switch(mMode) {
  215.                 case MODE_PENDING:
  216.                     break;
  217.                 case MODE_BILLING:
  218.                     if(((TextView)rowView.findViewById(R.id.item_text_job))!=null) {
  219.                         ((TextView)rowView.findViewById(R.id.item_text_job)).setVisibility(View.VISIBLE);
  220.                     }
  221.                     //rowView.forceLayout();
  222.                     break;
  223.                 default:
  224.                     break;
  225.             }
  226.            
  227.             //TODO: add code to resize the item when showing the job, as it is not done automagically.
  228.             //parent.forceLayout();
  229.            
  230.             return super.getGroupView(groupPosition, isExpanded, rowView, parent);
  231.         }
  232.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement