Android Open Source - Xpense New Entry






From Project

Back to project page Xpense.

License

The source code is released under:

MIT License

If you think the Android project Xpense 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 com.kevinzhu.xpense;
/*from   w  w w .  j  av  a2 s  .co m*/
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.Calendar;


public class NewEntry extends Activity {

    String expenseType;
    ArrayAdapter<CharSequence> sp_adapter;
    Spinner category_dropdown;
    Spinner subcategory_dropdown;
    String noteType;
    String listBeingEdited = ""; //Used for going back to the listview upon successful edit
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Main.parents.push(getClass());
        setContentView(R.layout.activity_new_entry);
        getActionBar().setDisplayHomeAsUpEnabled(true);


        final TextView selectExpense = (TextView) findViewById(R.id.expenseSelector);
        final TextView selectIncome = (TextView) findViewById(R.id.incomeSelector);

        final Context ctxt = getApplicationContext();

        final int colourActive = 0xFFCC5200;
        final int colourInactive = 0x46FFFFFF;

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);

        category_dropdown = (Spinner) findViewById(R.id.category);
        subcategory_dropdown = (Spinner) findViewById(R.id.subCategory);
        Spinner paymentSource = (Spinner) findViewById(R.id.paymentSource);
        EditText et_date = (EditText) findViewById(R.id.date);
        EditText et_notes   = (EditText)findViewById(R.id.notes);
        EditText et_amount   = (EditText)findViewById(R.id.amount);
        Button finishAdd = (Button) findViewById(R.id.finishAdd);



        Intent intent = getIntent();
        noteType = intent.getStringExtra("noteType");
        //Get all the expenses
        if (noteType.equals("new")){
            // If we're creating a new note, go through this setup
            //Either Expense or Income. This variable is hard to name...

            expenseType = "Expense";
            //Initialize the background of the expense/income chooser. **Set to expense initially
            selectExpense.setBackgroundColor(colourActive);
            selectIncome.setBackgroundColor(colourInactive);

            //Initialize date with today's date (keeps entries consistent nad prevents blank spaces

            et_date.setText(year + "-" + month + "-" + day);

            //Initialize the category spinner to list pertaining to expenses.
            //This will have to be changed once user switches between income and expense
            //Upon switching to income, the array to be displayed is grabbed from R.array.category_array_income
            sp_adapter = ArrayAdapter.createFromResource(this, R.array.category_array_expense, android.R.layout.simple_spinner_item);
            sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            category_dropdown.setAdapter(sp_adapter);

            //Initialize the subcategory to whatever is displayed when the category spinner is set to expense-related categories
            updateSubcategoryList(subcategory_dropdown, category_dropdown);

            //Initialize payment source
            sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_paymentSource, android.R.layout.simple_spinner_item);
            sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            paymentSource.setAdapter(sp_adapter);

            //Create row and push to Parse database
            finishAdd.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    EditText et_amount   = (EditText)findViewById(R.id.amount);
                    try{
                        Double amount = Double.parseDouble(et_amount.getText().toString());
                        if ( amount > 0 ) {

                            Spinner sp_category = (Spinner) findViewById(R.id.category);
                            Spinner et_subCategory = (Spinner) findViewById(R.id.subCategory);
                            Spinner et_paymentSource = (Spinner) findViewById(R.id.paymentSource);
                            EditText et_date = (EditText) findViewById(R.id.date);
                            EditText et_notes = (EditText) findViewById(R.id.notes);
                            ParseObject newPost;
                            if (expenseType.equals("Expense")) {
                                newPost = new ParseObject("Expense");
                                newPost.put("expenseType", "Expense");
                            } else {
                                newPost = new ParseObject("Income");
                                newPost.put("expenseType", "Income");
                            }
                            newPost.put("amount", et_amount.getText().toString());
                            newPost.put("category", sp_category.getSelectedItem().toString());
                            newPost.put("subCategory", et_subCategory.getSelectedItem().toString());
                            newPost.put("paymentSource", et_paymentSource.getSelectedItem().toString());
                            newPost.put("date", et_date.getText().toString());
                            newPost.put("notes", et_notes.getText().toString());
                            newPost.saveEventually();

                            CharSequence msg = "Added";
                            int duration = Toast.LENGTH_SHORT;
                            Toast toast = Toast.makeText(ctxt, msg, duration);
                            toast.show();

                            returnToAndRefreshParentIntent();
                        }
                        else{
                            CharSequence msg = "Invalid. Amount cannot be zero or negative";
                            int duration = Toast.LENGTH_SHORT;
                            Toast toast = Toast.makeText(ctxt, msg, duration);
                            toast.show();
                        }
                    } catch (NumberFormatException e){
                        CharSequence msg = "Invalid input";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(ctxt, msg, duration);
                        toast.show();
                    }


//                //Go back to main class
//                Intent ourIntent = new Intent(ctxt, Main.class);
//                startActivity(ourIntent);
                }
            });

            selectExpense.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    expenseType = "Expense";
                    selectExpense.setBackgroundColor(colourActive);
                    selectIncome.setBackgroundColor(colourInactive);

                    //Change spinner array sets
                    sp_adapter = ArrayAdapter.createFromResource(ctxt, R.array.category_array_expense, android.R.layout.simple_spinner_item);
                    sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    category_dropdown.setAdapter(sp_adapter);
                    updateSubcategoryList(subcategory_dropdown, category_dropdown);

                    //Toasts for debugging
                    CharSequence msg = "Adding new expense";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(ctxt, msg, duration);
                    toast.show();

                }
            });
            selectIncome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    expenseType = "Income";
                    selectExpense.setBackgroundColor(colourInactive);
                    selectIncome.setBackgroundColor(colourActive);

                    //Change spinner array sets
                    sp_adapter = ArrayAdapter.createFromResource(ctxt, R.array.category_array_income, android.R.layout.simple_spinner_item);
                    sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    category_dropdown.setAdapter(sp_adapter);
                    updateSubcategoryList(subcategory_dropdown, category_dropdown);

                    //Toasts for debugging
                    CharSequence msg = "Adding new income";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(ctxt, msg, duration);
                    toast.show();

                }
            });

        }
        else if (noteType.equals("editExisting")){
            Bundle extras = getIntent().getExtras();
            int index = extras.getInt("objectIndex");
            final CashFlow editingEntry = CashList.expenseArrayList.get(index);
            expenseType = editingEntry.getM_type();
            listBeingEdited = extras.getString("currentListType");
            if (expenseType.equals("Expense")){
                selectExpense.setBackgroundColor(colourActive);
                selectIncome.setBackgroundColor(colourInactive);
            }
            else{
                selectExpense.setBackgroundColor(colourInactive);
                selectIncome.setBackgroundColor(colourActive);
            }

            et_date.setText(editingEntry.getM_payDate());
            et_notes.setText(editingEntry.getM_comments());
            et_amount.setText(String.valueOf(editingEntry.getM_amount()));

            //Initialize the category spinner to list pertaining to expenses.
            //This will have to be changed once user switches between income and expense
            //Upon switching to income, the array to be displayed is grabbed from R.array.category_array_income

            if (expenseType.equals("Expense")){
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.category_array_expense, android.R.layout.simple_spinner_item);
            }
            else if (expenseType.equals("Income")){
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.category_array_income, android.R.layout.simple_spinner_item);
            }
            sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            category_dropdown.setAdapter(sp_adapter);

            category_dropdown.setSelection(sp_adapter.getPosition(editingEntry.getM_category()));

            //Initialize the subcategory to whatever is displayed when the category spinner is set to expense-related categories
            updateSubcategoryList(subcategory_dropdown, category_dropdown);
            subcategory_dropdown.setSelection(sp_adapter.getPosition(editingEntry.getM_subcategory()));


            sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_paymentSource, android.R.layout.simple_spinner_item);
            sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            paymentSource.setAdapter(sp_adapter);

            paymentSource.setSelection(sp_adapter.getPosition(editingEntry.getM_cashSource()));

            finishAdd.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    final EditText et_amount   = (EditText)findViewById(R.id.amount);
                    try {
                        Double amount = Double.parseDouble(et_amount.getText().toString());
                        if ( amount > 0 ) {
                            final Spinner sp_category = (Spinner) findViewById(R.id.category);
                            final Spinner et_subCategory = (Spinner) findViewById(R.id.subCategory);
                            final Spinner et_paymentSource = (Spinner) findViewById(R.id.paymentSource);
                            final EditText et_date = (EditText) findViewById(R.id.date);
                            final EditText et_notes = (EditText) findViewById(R.id.notes);

                            ParseQuery<ParseObject> query = ParseQuery.getQuery(expenseType);
                            String objectID = editingEntry.getM_objectID();
                            query.getInBackground(objectID, new GetCallback<ParseObject>() {
                                @Override
                                public void done(ParseObject editingObject, ParseException e) {
                                    if (e == null) {
                                        editingObject.put("amount", et_amount.getText().toString());
                                        editingObject.put("category", sp_category.getSelectedItem().toString());
                                        editingObject.put("subCategory", et_subCategory.getSelectedItem().toString());
                                        editingObject.put("paymentSource", et_paymentSource.getSelectedItem().toString());
                                        editingObject.put("date", et_date.getText().toString());
                                        editingObject.put("notes", et_notes.getText().toString());
                                        editingObject.saveEventually();
                                    }
                                }
                            });

                            CharSequence msg = "Edit has been made";
                            int duration = Toast.LENGTH_SHORT;
                            Toast toast = Toast.makeText(ctxt, msg, duration);
                            toast.show();

                            returnToAndRefreshParentIntent();
                        }
                        else{
                            CharSequence msg = "Invalid. Amount cannot be zero or negative";
                            int duration = Toast.LENGTH_SHORT;
                            Toast toast = Toast.makeText(ctxt, msg, duration);
                            toast.show();
                        }
                    } catch (NumberFormatException e){
                        CharSequence msg = "Invalid input";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(ctxt, msg, duration);
                        toast.show();
                    }
                }
            });

        }

        category_dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                updateSubcategoryList(subcategory_dropdown, category_dropdown);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                //Ignore, do nothing
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_entry, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
            case R.id.action_settings:
                return true;

            case android.R.id.home:
                Main.parents.pop();
                finish();

                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void showDatePickerDialog(View v) {
        DatePickerFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }
    public void updateDate(String date){
        EditText et_date   = (EditText)findViewById(R.id.date);
        et_date.setText(date);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        switch(keyCode){
            case KeyEvent.KEYCODE_BACK:
                Main.parents.pop();
                finish();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void returnToAndRefreshParentIntent (){
        Main.parents.pop(); //Prevent current activity from being registered as the parent
        Intent parentActivityIntent = new Intent(this, Main.parents.pop());
        parentActivityIntent.putExtra("type", listBeingEdited);
        parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(parentActivityIntent);
    }

    //Subcategory spinners depend on the selection made in the category spinner
    //This method will check what category is chosen at the time of call and
    //return the corresponding string array resource pertaining to the category
    public void updateSubcategoryList(Spinner subcategory_dropdown, Spinner category_dropdown){
        String sCategory = category_dropdown.getSelectedItem().toString();

        switch (sCategory){
            case "Car":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_car,android.R.layout.simple_spinner_item);
                break;
            case "Clothing":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_clothing,android.R.layout.simple_spinner_item);
                break;
            case "Education":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_education,android.R.layout.simple_spinner_item);
                break;
            case "Electronics":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_Electronics,android.R.layout.simple_spinner_item);
                break;
            case "Food":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_food,android.R.layout.simple_spinner_item);
                break;
            case "Gifts":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_gifts,android.R.layout.simple_spinner_item);
                break;
            case "Groceries":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_groceries,android.R.layout.simple_spinner_item);
                break;
            case "House":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_house,android.R.layout.simple_spinner_item);
                break;
            case "Insurance":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_insurance,android.R.layout.simple_spinner_item);
                break;
            case "Medical":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_medical,android.R.layout.simple_spinner_item);
                break;
            case "Misc":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_misc,android.R.layout.simple_spinner_item);
                break;
            case "Travel":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_travel,android.R.layout.simple_spinner_item);
                break;
            case "Utilities":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_utilities,android.R.layout.simple_spinner_item);
                break;

            case "Job Income":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_job,android.R.layout.simple_spinner_item);
                break;

            case "Misc Income":
                sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_none,android.R.layout.simple_spinner_item);
                break;

            default: sp_adapter = ArrayAdapter.createFromResource(this, R.array.subcategory_none,android.R.layout.simple_spinner_item);
                break;

        }
        //Set
        sp_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        subcategory_dropdown.setAdapter(sp_adapter);


    }
}




Java Source Code List

com.kevinzhu.xpense.ApplicationTest.java
com.kevinzhu.xpense.CashFlowListAdapter.java
com.kevinzhu.xpense.CashFlow.java
com.kevinzhu.xpense.CashList.java
com.kevinzhu.xpense.DatePickerFragment.java
com.kevinzhu.xpense.Main.java
com.kevinzhu.xpense.NewEntry.java
com.kevinzhu.xpense.SwipeDismissListViewTouchListener.java
com.kevinzhu.xpense.Xpense.java