Android Open Source - DisposableIncome-OldJava Cash Withdrawal Details Activity






From Project

Back to project page DisposableIncome-OldJava.

License

The source code is released under:

MIT License

If you think the Android project DisposableIncome-OldJava listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.co.wilka.disposableincome;
/*w ww  .  ja v  a2s.  c  om*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.*;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CashWithdrawalDetailsActivity extends Activity {

    public static final String CashWithdrawId = "CashWithdraw";
    public static final String CashWithdrawToEditId = "CashWithdrawToEdit";
    public static final int RESULT_DELETE_WITHDRAWAL = RESULT_FIRST_USER + 1;

    private WithdrawType withdrawType;
    private Date date;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_withdrawal);

        if(isEditingWithdrawal())
        {
            CashWithdraw toEdit = getCashWithdrawalToEdit();
            date = toEdit.getDate();
            withdrawType = toEdit.getWithdrawType();

            findViewById(R.id.deleteButton).setVisibility(View.VISIBLE);
            ((Button)findViewById(R.id.addButton)).setText("Update");
            ((EditText) findViewById(R.id.notesText)).setText(toEdit.getNotes());
            ((EditText) findViewById(R.id.amountText)).setText(String.valueOf(toEdit.getAmount().getPounds()));
            ((RadioButton)findViewById(R.id.radio_actual)).setChecked(toEdit.getWithdrawType() == WithdrawType.Actual);
            ((RadioButton)findViewById(R.id.radio_predicted)).setChecked(toEdit.getWithdrawType() == WithdrawType.Predicted);
        }
        else {
            findViewById(R.id.deleteButton).setVisibility(View.INVISIBLE);
            date = Calendar.getInstance().getTime();
            withdrawType = WithdrawType.Actual;
        }

        // List of events

        CalendarAccess calendarAccess = new CalendarAccess(this, ((ListView)findViewById(R.id.eventsList)));


        /*final ArrayList<String> list = new ArrayList<String>();
        list.add("2013-02-01: A thing");
        list.add("2013-02-07: Something else");
        list.add("2013-02-07: Different thing");
        list.add("2013-02-11: Blah");
        list.add("2013-02-11: More stuff");
        list.add("2013-02-11: Event more stuff");

        final ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
        ((ListView)findViewById(R.id.eventsList)).setAdapter(adapter);*/

        refreshDateButtonText();
    }

    private boolean isEditingWithdrawal() {
        return getCashWithdrawalToEdit() != null;
    }

    private CashWithdraw getCashWithdrawalToEdit() {

        // TODO: Check this still works after a pause->resume cycle
        // http://stackoverflow.com/questions/8565137/does-android-restore-intent-extras-when-resuming-activity
        // Does getExtras still work?

        CashWithdraw cashWithdrawToEdit = null;

        Bundle extras = this.getIntent().getExtras();
        if(extras != null) {
            Parcelable parcelable = extras.getParcelable(CashWithdrawToEditId);
            if(parcelable != null){
                cashWithdrawToEdit = (CashWithdraw) parcelable;
            }
        }
        return cashWithdrawToEdit;
    }

    public void onRadioButtonClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        switch(view.getId()) {
            case R.id.radio_actual:
                if (checked)
                    withdrawType = WithdrawType.Actual;
                    break;
            case R.id.radio_predicted:
                if (checked)
                    withdrawType = WithdrawType.Predicted;
                    break;
        }
    }

    public void dateClicked(View v) {
        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                date = new GregorianCalendar(year, month, dayOfMonth).getTime();
                refreshDateButtonText();
            }
        };

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, dateSetListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

        datePickerDialog.show();
    }

    private void refreshDateButtonText() {
        ((Button)findViewById(R.id.dateButton)).setText(DateFormat.getDateInstance().format(date));
    }

    public void addClicked(View v) {


        int amountInPounds;

        try {
            amountInPounds = Integer.parseInt(((EditText) findViewById(R.id.amountText)).getText().toString());
        }
        catch (NumberFormatException e) {
            Toast.makeText(this, "Not a valid amount", Toast.LENGTH_SHORT).show();
            return;
        }
        String notes = ((EditText) findViewById(R.id.notesText)).getText().toString();

        Intent data = new Intent();
        data.putExtra(CashWithdrawId, new CashWithdraw(date, Cash.fromPounds(amountInPounds), withdrawType, notes));

        CashWithdraw toEdit = getCashWithdrawalToEdit();
        if(toEdit != null) {
            data.putExtra(CashWithdrawToEditId, toEdit);
        }

        finishWithResultCode(data, Activity.RESULT_OK);
    }

    private void finishWithResultCode(Intent data, int resultCode) {
        if (getParent() == null) {
            setResult(resultCode, data);
        } else {
            getParent().setResult(resultCode, data);
        }
        finish();
    }

    public void deleteClicked(View v) {

        final CashWithdraw withdrawToDelete = getCashWithdrawalToEdit();

        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setMessage(String.format("Really delete this entry?"))
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent data = new Intent();
                        data.putExtra(CashWithdrawToEditId, withdrawToDelete);
                        finishWithResultCode(data, RESULT_DELETE_WITHDRAWAL);
                    }
                })
                .setNegativeButton("No", null)
                .show();
    }
}




Java Source Code List

uk.co.wilka.disposableincome.CalendarAccess.java
uk.co.wilka.disposableincome.CalendarProvider.java
uk.co.wilka.disposableincome.CashWithdraw.java
uk.co.wilka.disposableincome.CashWithdrawalDetailsActivity.java
uk.co.wilka.disposableincome.Cash.java
uk.co.wilka.disposableincome.DatabaseHelper.java
uk.co.wilka.disposableincome.IAccessData.java
uk.co.wilka.disposableincome.IProvideCalendar.java
uk.co.wilka.disposableincome.MainActivity.java
uk.co.wilka.disposableincome.PlusOneMonthCalendarProvider.java
uk.co.wilka.disposableincome.WithdrawDateComparer.java
uk.co.wilka.disposableincome.WithdrawType.java
uk.co.wilka.disposableincome.WithdrawalForMonthCalculator.java
uk.co.wilka.disposableincome.test.CashTest.java
uk.co.wilka.disposableincome.test.CashWithdrawTest.java
uk.co.wilka.disposableincome.test.FakeCalendarProvider.java
uk.co.wilka.disposableincome.test.PlusOneMonthCalendarProviderTests.java
uk.co.wilka.disposableincome.test.WithdrawDateComparerTests.java
uk.co.wilka.disposableincome.test.WithdrawalForMonthCalculatorTests.java