Advertisement
Rescue9

Untitled

Sep 7th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.11 KB | None | 0 0
  1. package com.corridor9design.mfdpaycalculator.deductions;
  2.  
  3. import com.corridor9design.mfdpaycalculator.R;
  4. import com.corridor9design.mfdpaycalculator.database.Deduction;
  5. import com.corridor9design.mfdpaycalculator.database.DeductionContentProvider;
  6.  
  7. import java.text.DecimalFormat;
  8.  
  9. import android.app.AlertDialog;
  10. import android.app.Dialog;
  11. import android.app.DialogFragment;
  12. import android.app.LoaderManager;
  13. import android.app.LoaderManager.LoaderCallbacks;
  14. import android.content.CursorLoader;
  15. import android.content.DialogInterface;
  16. import android.content.DialogInterface.OnClickListener;
  17. import android.content.Intent;
  18. import android.content.Loader;
  19. import android.database.Cursor;
  20. import android.os.Bundle;
  21. import android.view.View;
  22. import android.widget.AdapterView;
  23. import android.widget.AdapterView.OnItemClickListener;
  24. import android.widget.AdapterView.OnItemLongClickListener;
  25. import android.widget.ListView;
  26. import android.widget.SimpleCursorAdapter;
  27. import android.widget.TextView;
  28.  
  29. public class DeductionListDialog extends DialogFragment implements LoaderCallbacks<Cursor>, OnClickListener {
  30.  
  31.     // set the projection (what we're wanting from the database) to pass to the method
  32.     private static final String[] PROJECTION = new String[] { "_id", "name", "amount" };
  33.  
  34.     // set the loader's unique id. loader id's are specific to the Activity or Fragment in which they reside
  35.     private static final int LOADER_ID = 1;
  36.  
  37.     // the callback through which we will interact with the LoaderManager
  38.     private LoaderCallbacks<Cursor> mCallbacks;
  39.  
  40.     // the adapter that binds data to our listview
  41.     private SimpleCursorAdapter mAdapter;
  42.  
  43.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  44.         // the names of the columns we want to pass to the views
  45.         String[] DataColumns = new String[] { "name", "amount" };
  46.         // the views we want to pass the names to
  47.         int[] viewIDs = new int[] { R.id.deduction_listing_deduction_name, R.id.deduction_listing_deduction_amount };
  48.  
  49.         mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.deduction_listings, null, DataColumns, viewIDs, 0);
  50.        
  51.        
  52.         // set a binder so we can add a dollar sign in front of the amount
  53.         mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
  54.            
  55.             @Override
  56.             public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
  57.                 // if the column index is the amount column
  58.                 if (columnIndex == 2){
  59.                     // assign view to a new TextView
  60.                     TextView amount = (TextView) view;
  61.                     // create a number string from the cursor string in the amount column
  62.                     String number_string = cursor.getString(cursor.getColumnIndex("amount"));
  63.                     // setup decimal format
  64.                     DecimalFormat df = new DecimalFormat("$##.00");
  65.                     // create a new formatted string
  66.                     String my_new_amount = df.format(Double.parseDouble(number_string));
  67.                     // pass that new formatted string to the textview
  68.                     amount.setText(my_new_amount);
  69.                     return true;
  70.                 }
  71.                 return false;
  72.             }
  73.         });
  74.  
  75.         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
  76.  
  77.         // the main view that houses our layout
  78.         View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_deduction_list, null);
  79.  
  80.         // the listview that holds the deduction list
  81.         ListView listview = (ListView) view.findViewById(android.R.id.list);
  82.         listview.setOnItemClickListener(new OnItemClickListener() {
  83.  
  84.             @Override
  85.             // set a short click listener
  86.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  87.                 // create a new dialog fragment
  88.                 DialogFragment deduction_specifics = new DeductionSpecificsDialog();
  89.  
  90.                 // / bundle database row so we can get the correct info
  91.                 // for our specific listing
  92.                 Bundle arguments = new Bundle();
  93.                 arguments.putLong("database_row", id);
  94.                 deduction_specifics.setArguments(arguments);
  95.                 deduction_specifics.show(getFragmentManager(), "dialog");
  96.             }
  97.         });
  98.         // set the long click listener
  99.         listview.setOnItemLongClickListener(new OnItemLongClickListener() {
  100.  
  101.             // on long click we want to open the edit fragment
  102.             @Override
  103.             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  104.                 DialogFragment deduction_edit = new DeductionEditFragment();
  105.                 Bundle arguments = new Bundle();
  106.                 arguments.putLong("database_id", id);
  107.                 deduction_edit.setArguments(arguments);
  108.                 deduction_edit.show(getFragmentManager(), "dialog");
  109.                 /*Intent deduction_edit_intent = new Intent(getActivity(), DeductionEditActivity.class);
  110.                 deduction_edit_intent.putExtra("database_id", id);
  111.                 startActivity(deduction_edit_intent);*/
  112.                 return true;
  113.             }
  114.         });
  115.  
  116.         listview.setAdapter(mAdapter);
  117.  
  118.         alertDialogBuilder.setView(view);
  119.         alertDialogBuilder.setTitle("Deductions: ");
  120.         alertDialogBuilder.setMessage("Long press to update or delete");
  121.         alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  122.                public void onClick(DialogInterface dialog, int id) {
  123.                    // User clicked OK button
  124.                    dialog.dismiss();
  125.                }
  126.            });
  127.         alertDialogBuilder.setNegativeButton("Add", new DialogInterface.OnClickListener() {
  128.                public void onClick(DialogInterface dialog, int id) {
  129.                    // User clicked Add button
  130.                     DialogFragment deduction_edit = new DeductionEditFragment();
  131.                     Bundle arguments = new Bundle();
  132.                     deduction_edit.setArguments(arguments);
  133.                     deduction_edit.show(getFragmentManager(), "dialog");
  134.  
  135.                    /*Intent deduction = new Intent(getActivity(), DeductionEditActivity.class);
  136.                    startActivity(deduction);*/
  137.                }
  138.            });
  139.  
  140.         // The Activity (which implements the LoaderCallbacks<Cursor> interface) is the callbacks object through which
  141.         // we will interact with the LoaderManager. The LoaderManager uses this object to instantiate the Loader and to
  142.         // notify the client when data is made available/unavailable.
  143.         mCallbacks = this;
  144.  
  145.         // Initialize the Loader with id '1' and callbacks 'mCallbacks'. If the loader doesn't already exist, one is
  146.         // created. Otherwise, the already created Loader is reused. In either case, the LoaderManager will manage the
  147.         // Loader across the Activity/Fragment lifecycle, will receive any new loads once they have completed, and will
  148.         // report this new data back to the 'mCallbacks' object.
  149.         LoaderManager lm = getLoaderManager();
  150.         lm.initLoader(LOADER_ID, null, mCallbacks);
  151.  
  152.         AlertDialog dialog = alertDialogBuilder.create();
  153.         return dialog;
  154.  
  155.     }
  156.  
  157.     public void onResume() {
  158.         super.onResume();
  159.         LoaderManager lm = getLoaderManager();
  160.         lm.initLoader(LOADER_ID, null, mCallbacks);
  161.     }
  162.  
  163.     @Override
  164.     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  165.         // create a new CursorLoader with the following query parameters
  166.         return new CursorLoader(getActivity(), DeductionContentProvider.CONTENT_URI, PROJECTION, null, null,
  167.                 Deduction.COLUMN_NAME + " ASC");
  168.     }
  169.  
  170.     @Override
  171.     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
  172.         // a switch case is useful when dealing with multiple loaders/ids
  173.         switch (loader.getId()) {
  174.         case LOADER_ID:
  175.             // the asyncronous load is complete and the data is now available for use. Only now can we associate the
  176.             // query Cursor with the SimpleCursoeAdapter
  177.             mAdapter.swapCursor(cursor);
  178.             break;
  179.         }
  180.         // the listview now displays the queried data
  181.     }
  182.  
  183.     @Override
  184.     public void onLoaderReset(Loader<Cursor> loader) {
  185.         // for whatever reason, the loader's data is now unavailable. remove any references to the old data by replacing
  186.         // it with a null Cursor
  187.         mAdapter.swapCursor(null);
  188.     }
  189.  
  190.     @Override
  191.     public void onClick(DialogInterface dialog, int which) {
  192.         // TODO Auto-generated method stub
  193.        
  194.     }
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. package com.corridor9design.mfdpaycalculator.deductions;
  207.  
  208. import android.app.AlertDialog;
  209. import android.app.Dialog;
  210. import android.app.DialogFragment;
  211. import android.content.ContentResolver;
  212. import android.content.ContentUris;
  213. import android.content.ContentValues;
  214. import android.content.Context;
  215. import android.content.DialogInterface;
  216. import android.database.Cursor;
  217. import android.net.Uri;
  218. import android.os.Bundle;
  219. import android.util.Log;
  220. import android.view.View;
  221. import android.view.WindowManager;
  222. import android.view.View.OnClickListener;
  223. import android.widget.Button;
  224. import android.widget.CheckBox;
  225. import android.widget.EditText;
  226. import android.widget.Toast;
  227.  
  228. import com.corridor9design.mfdpaycalculator.BuildConfig;
  229. import com.corridor9design.mfdpaycalculator.MainActivity;
  230. import com.corridor9design.mfdpaycalculator.R;
  231. import com.corridor9design.mfdpaycalculator.database.Deduction;
  232. import com.corridor9design.mfdpaycalculator.database.DeductionContentProvider;
  233. import com.corridor9design.mfdpaycalculator.database.MyDeductionDbHelper;
  234.  
  235. public class DeductionEditFragment extends DialogFragment {
  236.  
  237.     // declare variables
  238.     String deduction_name;
  239.     String deduction_amount;
  240.     String deduction_number;
  241.     String deduction_description;
  242.     String first_payday;
  243.     String second_payday;
  244.     String third_payday;
  245.     long database_id;
  246.  
  247.     // declare gui elements
  248.     EditText deduction_name_edit;
  249.     EditText deduction_amount_edit;
  250.     EditText deduction_number_edit;
  251.     EditText deduction_description_edit;
  252.     CheckBox deduction_first_pay_checkbox;
  253.     CheckBox deduction_second_pay_checkbox;
  254.     CheckBox deduction_third_pay_checkbox;
  255.     //Button deduction_positive_button;
  256.     //Button deduction_negative_button;
  257.     //Button deduction_neutral_button;
  258.  
  259.     MyDeductionDbHelper db = new MyDeductionDbHelper(getActivity());
  260.  
  261.     ContentValues values = new ContentValues();
  262.     View view;
  263.    
  264.     AlertDialog.Builder mBuilder;
  265.  
  266.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  267.         super.onCreate(savedInstanceState);
  268.        
  269.         mBuilder = new AlertDialog.Builder(getActivity());
  270.         view = getActivity().getLayoutInflater().inflate(R.layout.activity_deduction_editor, null);
  271.         setupGuiInstances();
  272.         //startListening();
  273.        
  274.         mBuilder.setTitle("Edit Dialog");
  275.         mBuilder.setView(view);
  276.         mBuilder.setPositiveButton(R.string.deduction_button_cancel, new DialogInterface.OnClickListener() {
  277.  
  278.             @Override
  279.             public void onClick(DialogInterface dialog, int which) {
  280.                 dismiss();
  281.             }            
  282.         });
  283.                
  284.         mBuilder.setNegativeButton(R.string.deduction_button_accept, new DialogInterface.OnClickListener() {
  285.  
  286.             @Override
  287.             public void onClick(DialogInterface dialog, int which) {
  288.                 if (BuildConfig.DEBUG) Log.d(MainActivity.TAG, "Added Deduction");
  289.                 getValues();
  290.                 if(deduction_name_edit.getText().length()!=0 && deduction_amount_edit.getText().length()!=0){
  291.                     createDeductionItem();
  292.                 } else {
  293.                     Toast.makeText(getActivity(), "Deduction must have Name & Amount", Toast.LENGTH_LONG).show();
  294.                 }
  295.                 dismiss();
  296.             }
  297.            
  298.         });
  299.        
  300.         return mBuilder.create();
  301.     }
  302.    
  303.     public void onResume() {
  304.         super.onResume();
  305.        
  306.         // get the database id
  307.         long db_id = getArguments().getLong("database_id", -1);
  308.         System.out.println(db_id+"HERE");
  309.  
  310.         // if the id isn't negative, then we edit ti
  311.         if (db_id > -1) {
  312.             editDeduction(db_id);
  313.         }
  314.  
  315.     }
  316.    
  317.     public void setupGuiInstances() {
  318.         // gui display elements
  319.         deduction_name_edit = (EditText) view.findViewById(R.id.deduction_name);
  320.         deduction_amount_edit = (EditText) view.findViewById(R.id.deduction_amount);
  321.         deduction_number_edit = (EditText) view.findViewById(R.id.deduction_number);
  322.         deduction_description_edit = (EditText) view.findViewById(R.id.deduction_description);
  323.         deduction_first_pay_checkbox = (CheckBox) view.findViewById(R.id.deduction_checkbox_first_payday);
  324.         deduction_second_pay_checkbox = (CheckBox) view.findViewById(R.id.deduction_checkbox_second_payday);
  325.         deduction_third_pay_checkbox = (CheckBox) view.findViewById(R.id.deduction_checkbox_third_payday);
  326.         //deduction_positive_button = (Button) view.findViewById(R.id.deduction_positive_button);
  327.         //deduction_negative_button = (Button) view.findViewById(R.id.deduction_negative_button);
  328.         //deduction_neutral_button = (Button) view.findViewById(R.id.deduction_neutral_button);
  329.     }
  330.    
  331.     /*private void startListening() {
  332.         deduction_positive_button.setOnClickListener(new OnClickListener() {
  333.  
  334.             @Override
  335.             public void onClick(View v) {
  336.                 return;
  337.             }
  338.         });
  339.  
  340.         deduction_negative_button.setOnClickListener(new OnClickListener() {
  341.  
  342.             @Override
  343.             public void onClick(View arg0) {
  344.                 if (BuildConfig.DEBUG) Log.d(MainActivity.TAG, "Added Deduction");
  345.                 getValues();
  346.                 createDeductionItem();
  347.                 return;
  348.             }
  349.         });
  350.  
  351.         deduction_neutral_button.setOnClickListener(new OnClickListener() {
  352.  
  353.             @Override
  354.             public void onClick(View v) {
  355.                 clearValues();
  356.             }
  357.         });
  358.     }*/
  359.  
  360.     private void createDeductionItem() {
  361.         // create a resolver to connect to the content provider
  362.         ContentResolver resolver = getActivity().getContentResolver();
  363.  
  364.         // clear the ContentValues object; values
  365.         values.clear();
  366.  
  367.         values.put(Deduction.COLUMN_AMOUNT, deduction_amount);
  368.         values.put(Deduction.COLUMN_DESCRIPTION, deduction_description);
  369.         values.put(Deduction.COLUMN_NAME, deduction_name);
  370.         values.put(Deduction.COLUMN_NUMBER, deduction_number);
  371.         values.put(Deduction.COLUMN_PAYDAY1, first_payday);
  372.         values.put(Deduction.COLUMN_PAYDAY2, second_payday);
  373.         values.put(Deduction.COLUMN_PAYDAY3, third_payday);
  374.  
  375.         resolver.insert(DeductionContentProvider.CONTENT_URI, values);
  376.     }
  377.    
  378.     private void clearValues() {
  379.         deduction_name_edit.setText("");
  380.         deduction_amount_edit.setText("");
  381.         deduction_number_edit.setText("");
  382.         deduction_description_edit.setText("");
  383.         deduction_first_pay_checkbox.setChecked(false);
  384.         deduction_second_pay_checkbox.setChecked(false);
  385.         deduction_third_pay_checkbox.setChecked(false);
  386.     }
  387.    
  388.     private Dialog editDeduction(long id) {
  389.         // setup resolver to get from content provider
  390.         database_id = id;
  391.         ContentResolver resolver = getActivity().getContentResolver();
  392.  
  393.         // set arrays for querying database
  394.         String[] projection = new String[] { "_id", "name", "amount", "number", "description", "payday1", "payday2", "payday3" };
  395.         String[] selectionArgs = new String[] { id + "" };
  396.  
  397.         // clear previous values
  398.         values.clear();
  399.  
  400.         // get these columns into the content values object
  401.         values.get(Deduction.COLUMN_AMOUNT);
  402.         values.get(Deduction.COLUMN_DESCRIPTION);
  403.         values.get(Deduction.COLUMN_ID);
  404.         values.get(Deduction.COLUMN_NAME);
  405.         values.get(Deduction.COLUMN_NUMBER);
  406.         values.get(Deduction.COLUMN_PAYDAY1);
  407.         values.get(Deduction.COLUMN_PAYDAY2);
  408.         values.get(Deduction.COLUMN_PAYDAY3);
  409.  
  410.         // we want a singular row, so we'll create a new URI with the row id
  411.         Uri singleUri = ContentUris.withAppendedId(DeductionContentProvider.CONTENT_URI, id);
  412.  
  413.         // get the row from the content provider into a cursor
  414.         Cursor cursor = resolver.query(singleUri, projection, Deduction.COLUMN_ID + "=?", selectionArgs, null);
  415.         cursor.moveToFirst();
  416.  
  417.         // set the edittext areas to the cursor strings from the database
  418.         deduction_name_edit.setText(cursor.getString(1));
  419.         deduction_amount_edit.setText(cursor.getString(2));
  420.         deduction_number_edit.setText(cursor.getString(3));
  421.         deduction_description_edit.setText(cursor.getString(4));
  422.  
  423.         // use these if statements to check boxes as return results are strings
  424.         if (cursor.getString(5).equals("true")) {
  425.             deduction_first_pay_checkbox.setChecked(true);
  426.         }
  427.         if (cursor.getString(6).equals("true")) {
  428.             deduction_second_pay_checkbox.setChecked(true);
  429.         }
  430.         if (cursor.getString(7).equals("true")) {
  431.             deduction_third_pay_checkbox.setChecked(true);
  432.         }
  433.  
  434.         // change the names of the buttons since we're not adding a new item
  435.         mBuilder.setPositiveButton(R.string.deduction_button_delete, new DialogInterface.OnClickListener() {
  436.  
  437.             @Override
  438.             public void onClick(DialogInterface dialog, int which) {
  439.                 deleteDeductionItem();              
  440.             }            
  441.         });
  442.        
  443.         mBuilder.setNeutralButton(R.string.deduction_button_update, new DialogInterface.OnClickListener() {
  444.  
  445.             @Override
  446.             public void onClick(DialogInterface dialog, int which) {
  447.                 if(deduction_name_edit.getText().length()==0 && deduction_amount_edit.getText().length()==0){
  448.                     deleteDeductionItem();
  449.                     return;
  450.                 }
  451.                 updateDeductionItem();
  452.                 return;              
  453.             }
  454.            
  455.         });
  456.        
  457.         mBuilder.setNegativeButton(R.string.deduction_button_cancel, new DialogInterface.OnClickListener() {
  458.  
  459.             @Override
  460.             public void onClick(DialogInterface dialog, int which) {
  461.                 return;
  462.             }
  463.            
  464.         });
  465.        
  466.         Dialog editDialog = mBuilder.create();
  467.         return editDialog;
  468.     }
  469.    
  470.     private void updateDeductionItem() {
  471.         // get values from gui
  472.         getValues();
  473.         // create a resolver to connect to the content provider
  474.         ContentResolver resolver = getActivity().getContentResolver();
  475.         String[] selectionArgs = new String[] { database_id + "" };
  476.  
  477.         // clear values
  478.         values.clear();
  479.  
  480.         values.put(Deduction.COLUMN_AMOUNT, deduction_amount);
  481.         values.put(Deduction.COLUMN_DESCRIPTION, deduction_description);
  482.         values.put(Deduction.COLUMN_NAME, deduction_name);
  483.         values.put(Deduction.COLUMN_NUMBER, deduction_number);
  484.         values.put(Deduction.COLUMN_PAYDAY1, first_payday);
  485.         values.put(Deduction.COLUMN_PAYDAY2, second_payday);
  486.         values.put(Deduction.COLUMN_PAYDAY3, third_payday);
  487.  
  488.         resolver.update(DeductionContentProvider.CONTENT_URI, values, Deduction.COLUMN_ID + "=?", selectionArgs);
  489.     }
  490.  
  491.     // delete the deduction item from the database
  492.     private void deleteDeductionItem() {
  493.         // create a resolver to connect to the content provider
  494.         ContentResolver resolver = getActivity().getContentResolver();
  495.         String[] selectionArgs = new String[] { database_id + "" };
  496.  
  497.         resolver.delete(DeductionContentProvider.CONTENT_URI, Deduction.COLUMN_ID + "=?", selectionArgs);
  498.     }
  499.    
  500.     private void getValues() {
  501.         // get values from current input
  502.         deduction_name = deduction_name_edit.getText().toString();
  503.         deduction_amount = deduction_amount_edit.getText().toString();
  504.         deduction_number = deduction_number_edit.getText().toString();
  505.         deduction_description = deduction_description_edit.getText().toString();
  506.  
  507.         if (deduction_first_pay_checkbox.isChecked()) {
  508.             first_payday = "true";
  509.         } else
  510.             first_payday = "false";
  511.  
  512.         if (deduction_second_pay_checkbox.isChecked()) {
  513.             second_payday = "true";
  514.         } else
  515.             second_payday = "false";
  516.  
  517.         if (deduction_third_pay_checkbox.isChecked()) {
  518.             third_payday = "true";
  519.         } else
  520.             third_payday = "false";
  521.     }
  522. }
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533. /**
  534.  * Program: DeductionSpecificsDialog.java
  535.  * Programmer: Andrew Buskov
  536.  * Date: Jun 17, 2013
  537.  * Purpose: To create a dialog fragment for displaying
  538.  *  the specific information about a selected deduction.
  539.  */
  540.  
  541. package com.corridor9design.mfdpaycalculator.deductions;
  542.  
  543. import com.corridor9design.mfdpaycalculator.R;
  544. import com.corridor9design.mfdpaycalculator.database.Deduction;
  545. import com.corridor9design.mfdpaycalculator.database.DeductionContentProvider;
  546.  
  547. import android.app.AlertDialog;
  548. import android.app.Dialog;
  549. import android.app.DialogFragment;
  550. import android.content.ContentResolver;
  551. import android.content.ContentUris;
  552. import android.content.ContentValues;
  553. import android.content.DialogInterface;
  554. import android.content.DialogInterface.OnClickListener;
  555. import android.database.Cursor;
  556. import android.net.Uri;
  557. import android.os.Bundle;
  558. import android.view.View;
  559. import android.widget.CheckBox;
  560. import android.widget.TextView;
  561. import java.text.*;
  562.  
  563. public class DeductionSpecificsDialog extends DialogFragment {
  564.  
  565.     DecimalFormat df = new DecimalFormat("$##0.00");
  566.    
  567.     // declare variables
  568.     String deduction_name;
  569.     String deduction_amount;
  570.     String deduction_number;
  571.     String deduction_description;
  572.     String first_payday;
  573.     String second_payday;
  574.     String third_payday;
  575.     long database_id;
  576.  
  577.     // declare gui elements
  578.     TextView deduction_specific_amount;
  579.     TextView deduction_specific_number;
  580.     TextView deduction_specific_description;
  581.  
  582.     CheckBox deduction_first_payday;
  583.     CheckBox deduction_second_payday;
  584.     CheckBox deduction_third_payday;
  585.  
  586.     View view;
  587.  
  588.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  589.         super.onCreate(savedInstanceState);
  590.         // find out what row we're looking for from the bundle
  591.         database_id = getArguments().getLong("database_row");
  592.  
  593.         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
  594.         // specify the base view that we want out info to populate
  595.         view = getActivity().getLayoutInflater().inflate(R.layout.dialog_deduction_specifics, null);
  596.  
  597.         // setup gui
  598.         setupGuiElements();
  599.         getDeduction();
  600.  
  601.         alertDialogBuilder.setTitle(deduction_name);
  602.         alertDialogBuilder.setView(view);
  603.         alertDialogBuilder.setPositiveButton(R.string.deduction_button_ok, new OnClickListener() {
  604.  
  605.             @Override
  606.             public void onClick(DialogInterface dialog, int which) {
  607.                 return;
  608.             }
  609.         });
  610.         // we want have a delete button
  611.         /*alertDialogBuilder.setNegativeButton(R.string.deduction_button_delete, new OnClickListener() {
  612.  
  613.             @Override
  614.             public void onClick(DialogInterface dialog, int which) {
  615.                 deleteDeductionItem();
  616.             }
  617.         });*/
  618.         return alertDialogBuilder.create();
  619.     }
  620.  
  621.     public void onResume() {
  622.         super.onResume();
  623.     }
  624.  
  625.     public void setupGuiElements() {
  626.  
  627.         deduction_specific_amount = (TextView) view.findViewById(R.id.deduction_specific_amount);
  628.         deduction_specific_number = (TextView) view.findViewById(R.id.deduction_specific_number);
  629.         deduction_specific_description = (TextView) view.findViewById(R.id.deduction_specific_description);
  630.         deduction_first_payday = (CheckBox) view.findViewById(R.id.deduction_specific_checkbox_first_payday);
  631.         deduction_second_payday = (CheckBox) view.findViewById(R.id.deduction_specific_checkbox_second_payday);
  632.         deduction_third_payday = (CheckBox) view.findViewById(R.id.deduction_specific_checkbox_third_payday);
  633.     }
  634.  
  635.     public void getDeduction() {
  636.         ContentResolver resolver = getActivity().getContentResolver();
  637.         ContentValues values = new ContentValues();
  638.  
  639.         // set arrays for querying database
  640.         String[] projection = new String[] { "_id", "name", "amount", "number", "description", "payday1", "payday2", "payday3" };
  641.         String[] selectionArgs = new String[] { database_id + "" };
  642.  
  643.         values.clear();
  644.  
  645.         // get these columns into the content values object
  646.         values.get(Deduction.COLUMN_AMOUNT);
  647.         values.get(Deduction.COLUMN_NUMBER);
  648.         values.get(Deduction.COLUMN_DESCRIPTION);
  649.         values.get(Deduction.COLUMN_ID);
  650.         values.get(Deduction.COLUMN_NAME);
  651.         values.get(Deduction.COLUMN_PAYDAY1);
  652.         values.get(Deduction.COLUMN_PAYDAY2);
  653.         values.get(Deduction.COLUMN_PAYDAY3);
  654.  
  655.         // we want a singular row, so we'll create a new URI with the row id
  656.         Uri singleUri = ContentUris.withAppendedId(DeductionContentProvider.CONTENT_URI, database_id);
  657.  
  658.         // get the row from the content provider into a cursor
  659.         Cursor cursor = resolver.query(singleUri, projection, Deduction.COLUMN_ID + "=?", selectionArgs, null);
  660.         cursor.moveToFirst();
  661.  
  662.         deduction_name = cursor.getString(1);
  663.         deduction_specific_amount.setText(df.format(Double.parseDouble(cursor.getString(2))));
  664.         deduction_specific_number.setText(cursor.getString(3));
  665.         deduction_specific_description.setText(cursor.getString(4));
  666.  
  667.         // use these if statements to check boxes as return results are strings
  668.         if (cursor.getString(5).equals("true")) {
  669.             deduction_first_payday.setChecked(true);
  670.         }
  671.         if (cursor.getString(6).equals("true")) {
  672.             deduction_second_payday.setChecked(true);
  673.         }
  674.         if (cursor.getString(7).equals("true")) {
  675.             deduction_third_payday.setChecked(true);
  676.         }
  677.     }
  678. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement