Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.corridor9design.mfdpaycalculator.deductions;
- import com.corridor9design.mfdpaycalculator.R;
- import com.corridor9design.mfdpaycalculator.database.Deduction;
- import com.corridor9design.mfdpaycalculator.database.DeductionContentProvider;
- import java.text.DecimalFormat;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.app.LoaderManager;
- import android.app.LoaderManager.LoaderCallbacks;
- import android.content.CursorLoader;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent;
- import android.content.Loader;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemLongClickListener;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- import android.widget.TextView;
- public class DeductionListDialog extends DialogFragment implements LoaderCallbacks<Cursor>, OnClickListener {
- // set the projection (what we're wanting from the database) to pass to the method
- private static final String[] PROJECTION = new String[] { "_id", "name", "amount" };
- // set the loader's unique id. loader id's are specific to the Activity or Fragment in which they reside
- private static final int LOADER_ID = 1;
- // the callback through which we will interact with the LoaderManager
- private LoaderCallbacks<Cursor> mCallbacks;
- // the adapter that binds data to our listview
- private SimpleCursorAdapter mAdapter;
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- // the names of the columns we want to pass to the views
- String[] DataColumns = new String[] { "name", "amount" };
- // the views we want to pass the names to
- int[] viewIDs = new int[] { R.id.deduction_listing_deduction_name, R.id.deduction_listing_deduction_amount };
- mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.deduction_listings, null, DataColumns, viewIDs, 0);
- // set a binder so we can add a dollar sign in front of the amount
- mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
- @Override
- public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
- // if the column index is the amount column
- if (columnIndex == 2){
- // assign view to a new TextView
- TextView amount = (TextView) view;
- // create a number string from the cursor string in the amount column
- String number_string = cursor.getString(cursor.getColumnIndex("amount"));
- // setup decimal format
- DecimalFormat df = new DecimalFormat("$##.00");
- // create a new formatted string
- String my_new_amount = df.format(Double.parseDouble(number_string));
- // pass that new formatted string to the textview
- amount.setText(my_new_amount);
- return true;
- }
- return false;
- }
- });
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
- // the main view that houses our layout
- View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_deduction_list, null);
- // the listview that holds the deduction list
- ListView listview = (ListView) view.findViewById(android.R.id.list);
- listview.setOnItemClickListener(new OnItemClickListener() {
- @Override
- // set a short click listener
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- // create a new dialog fragment
- DialogFragment deduction_specifics = new DeductionSpecificsDialog();
- // / bundle database row so we can get the correct info
- // for our specific listing
- Bundle arguments = new Bundle();
- arguments.putLong("database_row", id);
- deduction_specifics.setArguments(arguments);
- deduction_specifics.show(getFragmentManager(), "dialog");
- }
- });
- // set the long click listener
- listview.setOnItemLongClickListener(new OnItemLongClickListener() {
- // on long click we want to open the edit fragment
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
- DialogFragment deduction_edit = new DeductionEditFragment();
- Bundle arguments = new Bundle();
- arguments.putLong("database_id", id);
- deduction_edit.setArguments(arguments);
- deduction_edit.show(getFragmentManager(), "dialog");
- /*Intent deduction_edit_intent = new Intent(getActivity(), DeductionEditActivity.class);
- deduction_edit_intent.putExtra("database_id", id);
- startActivity(deduction_edit_intent);*/
- return true;
- }
- });
- listview.setAdapter(mAdapter);
- alertDialogBuilder.setView(view);
- alertDialogBuilder.setTitle("Deductions: ");
- alertDialogBuilder.setMessage("Long press to update or delete");
- alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // User clicked OK button
- dialog.dismiss();
- }
- });
- alertDialogBuilder.setNegativeButton("Add", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- // User clicked Add button
- DialogFragment deduction_edit = new DeductionEditFragment();
- Bundle arguments = new Bundle();
- deduction_edit.setArguments(arguments);
- deduction_edit.show(getFragmentManager(), "dialog");
- /*Intent deduction = new Intent(getActivity(), DeductionEditActivity.class);
- startActivity(deduction);*/
- }
- });
- // The Activity (which implements the LoaderCallbacks<Cursor> interface) is the callbacks object through which
- // we will interact with the LoaderManager. The LoaderManager uses this object to instantiate the Loader and to
- // notify the client when data is made available/unavailable.
- mCallbacks = this;
- // Initialize the Loader with id '1' and callbacks 'mCallbacks'. If the loader doesn't already exist, one is
- // created. Otherwise, the already created Loader is reused. In either case, the LoaderManager will manage the
- // Loader across the Activity/Fragment lifecycle, will receive any new loads once they have completed, and will
- // report this new data back to the 'mCallbacks' object.
- LoaderManager lm = getLoaderManager();
- lm.initLoader(LOADER_ID, null, mCallbacks);
- AlertDialog dialog = alertDialogBuilder.create();
- return dialog;
- }
- public void onResume() {
- super.onResume();
- LoaderManager lm = getLoaderManager();
- lm.initLoader(LOADER_ID, null, mCallbacks);
- }
- @Override
- public Loader<Cursor> onCreateLoader(int id, Bundle args) {
- // create a new CursorLoader with the following query parameters
- return new CursorLoader(getActivity(), DeductionContentProvider.CONTENT_URI, PROJECTION, null, null,
- Deduction.COLUMN_NAME + " ASC");
- }
- @Override
- public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
- // a switch case is useful when dealing with multiple loaders/ids
- switch (loader.getId()) {
- case LOADER_ID:
- // the asyncronous load is complete and the data is now available for use. Only now can we associate the
- // query Cursor with the SimpleCursoeAdapter
- mAdapter.swapCursor(cursor);
- break;
- }
- // the listview now displays the queried data
- }
- @Override
- public void onLoaderReset(Loader<Cursor> loader) {
- // for whatever reason, the loader's data is now unavailable. remove any references to the old data by replacing
- // it with a null Cursor
- mAdapter.swapCursor(null);
- }
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- }
- package com.corridor9design.mfdpaycalculator.deductions;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.ContentResolver;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.corridor9design.mfdpaycalculator.BuildConfig;
- import com.corridor9design.mfdpaycalculator.MainActivity;
- import com.corridor9design.mfdpaycalculator.R;
- import com.corridor9design.mfdpaycalculator.database.Deduction;
- import com.corridor9design.mfdpaycalculator.database.DeductionContentProvider;
- import com.corridor9design.mfdpaycalculator.database.MyDeductionDbHelper;
- public class DeductionEditFragment extends DialogFragment {
- // declare variables
- String deduction_name;
- String deduction_amount;
- String deduction_number;
- String deduction_description;
- String first_payday;
- String second_payday;
- String third_payday;
- long database_id;
- // declare gui elements
- EditText deduction_name_edit;
- EditText deduction_amount_edit;
- EditText deduction_number_edit;
- EditText deduction_description_edit;
- CheckBox deduction_first_pay_checkbox;
- CheckBox deduction_second_pay_checkbox;
- CheckBox deduction_third_pay_checkbox;
- //Button deduction_positive_button;
- //Button deduction_negative_button;
- //Button deduction_neutral_button;
- MyDeductionDbHelper db = new MyDeductionDbHelper(getActivity());
- ContentValues values = new ContentValues();
- View view;
- AlertDialog.Builder mBuilder;
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mBuilder = new AlertDialog.Builder(getActivity());
- view = getActivity().getLayoutInflater().inflate(R.layout.activity_deduction_editor, null);
- setupGuiInstances();
- //startListening();
- mBuilder.setTitle("Edit Dialog");
- mBuilder.setView(view);
- mBuilder.setPositiveButton(R.string.deduction_button_cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dismiss();
- }
- });
- mBuilder.setNegativeButton(R.string.deduction_button_accept, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (BuildConfig.DEBUG) Log.d(MainActivity.TAG, "Added Deduction");
- getValues();
- if(deduction_name_edit.getText().length()!=0 && deduction_amount_edit.getText().length()!=0){
- createDeductionItem();
- } else {
- Toast.makeText(getActivity(), "Deduction must have Name & Amount", Toast.LENGTH_LONG).show();
- }
- dismiss();
- }
- });
- return mBuilder.create();
- }
- public void onResume() {
- super.onResume();
- // get the database id
- long db_id = getArguments().getLong("database_id", -1);
- System.out.println(db_id+"HERE");
- // if the id isn't negative, then we edit ti
- if (db_id > -1) {
- editDeduction(db_id);
- }
- }
- public void setupGuiInstances() {
- // gui display elements
- deduction_name_edit = (EditText) view.findViewById(R.id.deduction_name);
- deduction_amount_edit = (EditText) view.findViewById(R.id.deduction_amount);
- deduction_number_edit = (EditText) view.findViewById(R.id.deduction_number);
- deduction_description_edit = (EditText) view.findViewById(R.id.deduction_description);
- deduction_first_pay_checkbox = (CheckBox) view.findViewById(R.id.deduction_checkbox_first_payday);
- deduction_second_pay_checkbox = (CheckBox) view.findViewById(R.id.deduction_checkbox_second_payday);
- deduction_third_pay_checkbox = (CheckBox) view.findViewById(R.id.deduction_checkbox_third_payday);
- //deduction_positive_button = (Button) view.findViewById(R.id.deduction_positive_button);
- //deduction_negative_button = (Button) view.findViewById(R.id.deduction_negative_button);
- //deduction_neutral_button = (Button) view.findViewById(R.id.deduction_neutral_button);
- }
- /*private void startListening() {
- deduction_positive_button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- return;
- }
- });
- deduction_negative_button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- if (BuildConfig.DEBUG) Log.d(MainActivity.TAG, "Added Deduction");
- getValues();
- createDeductionItem();
- return;
- }
- });
- deduction_neutral_button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- clearValues();
- }
- });
- }*/
- private void createDeductionItem() {
- // create a resolver to connect to the content provider
- ContentResolver resolver = getActivity().getContentResolver();
- // clear the ContentValues object; values
- values.clear();
- values.put(Deduction.COLUMN_AMOUNT, deduction_amount);
- values.put(Deduction.COLUMN_DESCRIPTION, deduction_description);
- values.put(Deduction.COLUMN_NAME, deduction_name);
- values.put(Deduction.COLUMN_NUMBER, deduction_number);
- values.put(Deduction.COLUMN_PAYDAY1, first_payday);
- values.put(Deduction.COLUMN_PAYDAY2, second_payday);
- values.put(Deduction.COLUMN_PAYDAY3, third_payday);
- resolver.insert(DeductionContentProvider.CONTENT_URI, values);
- }
- private void clearValues() {
- deduction_name_edit.setText("");
- deduction_amount_edit.setText("");
- deduction_number_edit.setText("");
- deduction_description_edit.setText("");
- deduction_first_pay_checkbox.setChecked(false);
- deduction_second_pay_checkbox.setChecked(false);
- deduction_third_pay_checkbox.setChecked(false);
- }
- private Dialog editDeduction(long id) {
- // setup resolver to get from content provider
- database_id = id;
- ContentResolver resolver = getActivity().getContentResolver();
- // set arrays for querying database
- String[] projection = new String[] { "_id", "name", "amount", "number", "description", "payday1", "payday2", "payday3" };
- String[] selectionArgs = new String[] { id + "" };
- // clear previous values
- values.clear();
- // get these columns into the content values object
- values.get(Deduction.COLUMN_AMOUNT);
- values.get(Deduction.COLUMN_DESCRIPTION);
- values.get(Deduction.COLUMN_ID);
- values.get(Deduction.COLUMN_NAME);
- values.get(Deduction.COLUMN_NUMBER);
- values.get(Deduction.COLUMN_PAYDAY1);
- values.get(Deduction.COLUMN_PAYDAY2);
- values.get(Deduction.COLUMN_PAYDAY3);
- // we want a singular row, so we'll create a new URI with the row id
- Uri singleUri = ContentUris.withAppendedId(DeductionContentProvider.CONTENT_URI, id);
- // get the row from the content provider into a cursor
- Cursor cursor = resolver.query(singleUri, projection, Deduction.COLUMN_ID + "=?", selectionArgs, null);
- cursor.moveToFirst();
- // set the edittext areas to the cursor strings from the database
- deduction_name_edit.setText(cursor.getString(1));
- deduction_amount_edit.setText(cursor.getString(2));
- deduction_number_edit.setText(cursor.getString(3));
- deduction_description_edit.setText(cursor.getString(4));
- // use these if statements to check boxes as return results are strings
- if (cursor.getString(5).equals("true")) {
- deduction_first_pay_checkbox.setChecked(true);
- }
- if (cursor.getString(6).equals("true")) {
- deduction_second_pay_checkbox.setChecked(true);
- }
- if (cursor.getString(7).equals("true")) {
- deduction_third_pay_checkbox.setChecked(true);
- }
- // change the names of the buttons since we're not adding a new item
- mBuilder.setPositiveButton(R.string.deduction_button_delete, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- deleteDeductionItem();
- }
- });
- mBuilder.setNeutralButton(R.string.deduction_button_update, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if(deduction_name_edit.getText().length()==0 && deduction_amount_edit.getText().length()==0){
- deleteDeductionItem();
- return;
- }
- updateDeductionItem();
- return;
- }
- });
- mBuilder.setNegativeButton(R.string.deduction_button_cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
- });
- Dialog editDialog = mBuilder.create();
- return editDialog;
- }
- private void updateDeductionItem() {
- // get values from gui
- getValues();
- // create a resolver to connect to the content provider
- ContentResolver resolver = getActivity().getContentResolver();
- String[] selectionArgs = new String[] { database_id + "" };
- // clear values
- values.clear();
- values.put(Deduction.COLUMN_AMOUNT, deduction_amount);
- values.put(Deduction.COLUMN_DESCRIPTION, deduction_description);
- values.put(Deduction.COLUMN_NAME, deduction_name);
- values.put(Deduction.COLUMN_NUMBER, deduction_number);
- values.put(Deduction.COLUMN_PAYDAY1, first_payday);
- values.put(Deduction.COLUMN_PAYDAY2, second_payday);
- values.put(Deduction.COLUMN_PAYDAY3, third_payday);
- resolver.update(DeductionContentProvider.CONTENT_URI, values, Deduction.COLUMN_ID + "=?", selectionArgs);
- }
- // delete the deduction item from the database
- private void deleteDeductionItem() {
- // create a resolver to connect to the content provider
- ContentResolver resolver = getActivity().getContentResolver();
- String[] selectionArgs = new String[] { database_id + "" };
- resolver.delete(DeductionContentProvider.CONTENT_URI, Deduction.COLUMN_ID + "=?", selectionArgs);
- }
- private void getValues() {
- // get values from current input
- deduction_name = deduction_name_edit.getText().toString();
- deduction_amount = deduction_amount_edit.getText().toString();
- deduction_number = deduction_number_edit.getText().toString();
- deduction_description = deduction_description_edit.getText().toString();
- if (deduction_first_pay_checkbox.isChecked()) {
- first_payday = "true";
- } else
- first_payday = "false";
- if (deduction_second_pay_checkbox.isChecked()) {
- second_payday = "true";
- } else
- second_payday = "false";
- if (deduction_third_pay_checkbox.isChecked()) {
- third_payday = "true";
- } else
- third_payday = "false";
- }
- }
- /**
- * Program: DeductionSpecificsDialog.java
- * Programmer: Andrew Buskov
- * Date: Jun 17, 2013
- * Purpose: To create a dialog fragment for displaying
- * the specific information about a selected deduction.
- */
- package com.corridor9design.mfdpaycalculator.deductions;
- import com.corridor9design.mfdpaycalculator.R;
- import com.corridor9design.mfdpaycalculator.database.Deduction;
- import com.corridor9design.mfdpaycalculator.database.DeductionContentProvider;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.ContentResolver;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import java.text.*;
- public class DeductionSpecificsDialog extends DialogFragment {
- DecimalFormat df = new DecimalFormat("$##0.00");
- // declare variables
- String deduction_name;
- String deduction_amount;
- String deduction_number;
- String deduction_description;
- String first_payday;
- String second_payday;
- String third_payday;
- long database_id;
- // declare gui elements
- TextView deduction_specific_amount;
- TextView deduction_specific_number;
- TextView deduction_specific_description;
- CheckBox deduction_first_payday;
- CheckBox deduction_second_payday;
- CheckBox deduction_third_payday;
- View view;
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // find out what row we're looking for from the bundle
- database_id = getArguments().getLong("database_row");
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
- // specify the base view that we want out info to populate
- view = getActivity().getLayoutInflater().inflate(R.layout.dialog_deduction_specifics, null);
- // setup gui
- setupGuiElements();
- getDeduction();
- alertDialogBuilder.setTitle(deduction_name);
- alertDialogBuilder.setView(view);
- alertDialogBuilder.setPositiveButton(R.string.deduction_button_ok, new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
- });
- // we want have a delete button
- /*alertDialogBuilder.setNegativeButton(R.string.deduction_button_delete, new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- deleteDeductionItem();
- }
- });*/
- return alertDialogBuilder.create();
- }
- public void onResume() {
- super.onResume();
- }
- public void setupGuiElements() {
- deduction_specific_amount = (TextView) view.findViewById(R.id.deduction_specific_amount);
- deduction_specific_number = (TextView) view.findViewById(R.id.deduction_specific_number);
- deduction_specific_description = (TextView) view.findViewById(R.id.deduction_specific_description);
- deduction_first_payday = (CheckBox) view.findViewById(R.id.deduction_specific_checkbox_first_payday);
- deduction_second_payday = (CheckBox) view.findViewById(R.id.deduction_specific_checkbox_second_payday);
- deduction_third_payday = (CheckBox) view.findViewById(R.id.deduction_specific_checkbox_third_payday);
- }
- public void getDeduction() {
- ContentResolver resolver = getActivity().getContentResolver();
- ContentValues values = new ContentValues();
- // set arrays for querying database
- String[] projection = new String[] { "_id", "name", "amount", "number", "description", "payday1", "payday2", "payday3" };
- String[] selectionArgs = new String[] { database_id + "" };
- values.clear();
- // get these columns into the content values object
- values.get(Deduction.COLUMN_AMOUNT);
- values.get(Deduction.COLUMN_NUMBER);
- values.get(Deduction.COLUMN_DESCRIPTION);
- values.get(Deduction.COLUMN_ID);
- values.get(Deduction.COLUMN_NAME);
- values.get(Deduction.COLUMN_PAYDAY1);
- values.get(Deduction.COLUMN_PAYDAY2);
- values.get(Deduction.COLUMN_PAYDAY3);
- // we want a singular row, so we'll create a new URI with the row id
- Uri singleUri = ContentUris.withAppendedId(DeductionContentProvider.CONTENT_URI, database_id);
- // get the row from the content provider into a cursor
- Cursor cursor = resolver.query(singleUri, projection, Deduction.COLUMN_ID + "=?", selectionArgs, null);
- cursor.moveToFirst();
- deduction_name = cursor.getString(1);
- deduction_specific_amount.setText(df.format(Double.parseDouble(cursor.getString(2))));
- deduction_specific_number.setText(cursor.getString(3));
- deduction_specific_description.setText(cursor.getString(4));
- // use these if statements to check boxes as return results are strings
- if (cursor.getString(5).equals("true")) {
- deduction_first_payday.setChecked(true);
- }
- if (cursor.getString(6).equals("true")) {
- deduction_second_payday.setChecked(true);
- }
- if (cursor.getString(7).equals("true")) {
- deduction_third_payday.setChecked(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement