Android Open Source - Xpense Main






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;
//  ww w . j a va2s.c  om
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Stack;


public class Main extends Activity {

    public static Stack<Class<?>> parents = new Stack<Class<?>>();

    Context ctxt = this;
    double expense_all;
    double expense_all_credit;
    double expense_all_cash;

    double expense_month;
    double expense_month_credit;
    double expense_month_cash;

    double income_all;
    double income_all_credit;
    double income_all_cash;

    double income_month;
    double income_month_credit;
    double income_month_cash;

    Calendar cal = Calendar.getInstance();
    String thisMonth = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parents.push(getClass());
        setContentView(R.layout.activity_main);

        final DecimalFormat df = new DecimalFormat("#0.00");

        Button addButton = (Button) findViewById(R.id.addbutton);
        addButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent newEntry = new Intent(ctxt, NewEntry.class);
                newEntry.putExtra("noteType", "new");
                startActivity(newEntry);
            }
        });

        //Textviews are used as buttons to go to lists displaying total/monthly expenses/income
        TextView total_expenseList = (TextView) findViewById(R.id.totalExpenseList);
        final TextView totalExpense_all = (TextView)findViewById(R.id.expense_total_all);
        final TextView totalExpense_credit = (TextView) findViewById(R.id.totalExpense_credit);
        final TextView totalExpense_cash = (TextView) findViewById(R.id.totalExpense_cash);

        TextView total_incomeList = (TextView) findViewById(R.id.totalIncomeList);
        final TextView totalIncome_all = (TextView)findViewById(R.id.income_total_all);
        final TextView totalIncome_credit = (TextView) findViewById(R.id.totalIncome_credit);
        final TextView totalIncome_cash = (TextView) findViewById(R.id.totalIncome_cash);

        TextView monthly_expenseList = (TextView) findViewById(R.id.monthlyExpenseList);
        final TextView monthlyExpense_all = (TextView)findViewById(R.id.expense_total_monthly);
        final TextView monthlyExpense_credit = (TextView) findViewById(R.id.monthlyExpense_credit);
        final TextView monthlyExpense_cash = (TextView) findViewById(R.id.monthlyExpense_cash);

        TextView monthly_incomeList = (TextView) findViewById(R.id.monthlyIncomeList);
        final TextView monthlyIncome_all = (TextView)findViewById(R.id.income_total_monthly);
        final TextView monthlyIncome_credit = (TextView) findViewById(R.id.monthlyIncome_credit);
        final TextView monthlyIncome_cash = (TextView) findViewById(R.id.monthlyIncome_cash);

        Button viewExpenseButton = (Button) findViewById(R.id.viewExpenseButton);
        Button viewIncomeButton = (Button) findViewById(R.id.viewIncomeButton);

        //Textviews displaying cash amounts (income/expense)
        //total means all expenses, all means from both credit and cash





        ParseQuery<ParseObject> pq_listExpense;
        ParseQuery<ParseObject> pq_listIncome;

        //Get all the expenses

        pq_listExpense = ParseQuery.getQuery("Expense");

        pq_listIncome = ParseQuery.getQuery("Income");

        //Get database values to update $$
        pq_listExpense.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> entryList, ParseException e) {
                if (e == null){
                    //Retrieval of $ data
                    String month;
                    String date;
                    for (int i = 0; i < entryList.size(); i++){
                        try {
                            expense_all += Double.parseDouble(entryList.get(i).getString("amount"));
                            if (entryList.get(i).getString("paymentSource").equals("Credit")){
                                expense_all_credit += Double.parseDouble(entryList.get(i).getString("amount"));
                            }
                            //Using else if for future proofing in case new payment is added
                            else if (entryList.get(i).getString("paymentSource").equals("Cash")){
                                expense_all_cash += Double.parseDouble(entryList.get(i).getString("amount"));
                            }
                            date = entryList.get(i).getString("date");
                            if ((date.indexOf("-") + 1) != -1) {
                                month = date.substring(0, (date.indexOf("-") + 2)); //Would look like 2015-1
                                if (month.equals(thisMonth)) {
                                    expense_month += Double.parseDouble(entryList.get(i).getString("amount"));
                                    if (entryList.get(i).getString("paymentSource").equals("Credit")){
                                        expense_month_credit += Double.parseDouble(entryList.get(i).getString("amount"));
                                    }
                                    //Using else if for future proofing in case new payment is added
                                    else if (entryList.get(i).getString("paymentSource").equals("Cash")){
                                        expense_month_cash += Double.parseDouble(entryList.get(i).getString("amount"));
                                    }
                                }
                            }

                        } catch(NumberFormatException nfe){
                            //Ignore
                        }

                    }
                    totalExpense_all.setText("$"+String.valueOf(df.format(expense_all)));
                    totalExpense_all.setTextColor(getResources().getColor(R.color.expense_red));

                    totalExpense_credit.setText("$"+String.valueOf(df.format(expense_all_credit)));
                    totalExpense_credit.setTextColor(getResources().getColor(R.color.credit_cash_orange));

                    totalExpense_cash.setText("$"+String.valueOf(df.format(expense_all_cash)));
                    totalExpense_cash.setTextColor(getResources().getColor(R.color.credit_cash_orange));

                    monthlyExpense_all.setText("$"+String.valueOf(df.format(expense_month)));
                    monthlyExpense_all.setTextColor(getResources().getColor(R.color.expense_red));

                    monthlyExpense_credit.setText("$"+String.valueOf(df.format(expense_month_credit)));
                    monthlyExpense_credit.setTextColor(getResources().getColor(R.color.credit_cash_orange));

                    monthlyExpense_cash.setText("$"+String.valueOf(df.format(expense_month_cash)));
                    monthlyExpense_cash.setTextColor(getResources().getColor(R.color.credit_cash_orange));



                }
                else{
                    Log.d("Main Page", "Error: " + e.getMessage());
                }
            }

        });

        pq_listIncome.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> entryList, ParseException e) {
                if (e == null){
                    //Retrieval of $ data
                    String month;
                    String date;
                    for (int i = 0; i < entryList.size(); i++){
                        try {
                            income_all += Double.parseDouble(entryList.get(i).getString("amount"));

                            if (entryList.get(i).getString("paymentSource").equals("Credit")){
                                income_all_credit += Double.parseDouble(entryList.get(i).getString("amount"));
                            }
                            //Using else if for future proofing in case new payment is added
                            else if (entryList.get(i).getString("paymentSource").equals("Cash")){
                                income_all_cash += Double.parseDouble(entryList.get(i).getString("amount"));
                            }
                            date = entryList.get(i).getString("date");
                            if ((date.indexOf("-") + 1) != -1) {
                                month = date.substring(0, (date.indexOf("-") + 2)); //Would look like 2015-1
                                if (month.equals(thisMonth)) {
                                    income_month += Double.parseDouble(entryList.get(i).getString("amount"));
                                    if (entryList.get(i).getString("paymentSource").equals("Credit")){
                                        income_month_credit += Double.parseDouble(entryList.get(i).getString("amount"));
                                    }
                                    //Using else if for future proofing in case new payment is added
                                    else if (entryList.get(i).getString("paymentSource").equals("Cash")){
                                        income_month_cash += Double.parseDouble(entryList.get(i).getString("amount"));
                                    }
                                }
                            }
                        } catch(NumberFormatException nfe){
                            //Ignore
                        }

                    }
                    totalIncome_all.setText("$"+String.valueOf(df.format(income_all)));
                    totalIncome_all.setTextColor(getResources().getColor(R.color.income_green));

                    totalIncome_credit.setText("$"+String.valueOf(df.format(income_all_credit)));
                    totalIncome_credit.setTextColor(getResources().getColor(R.color.credit_cash_orange));

                    totalIncome_cash.setText("$"+String.valueOf(df.format(income_all_cash)));
                    totalIncome_cash.setTextColor(getResources().getColor(R.color.credit_cash_orange));

                    monthlyIncome_all.setText("$"+String.valueOf(df.format(income_month)));
                    monthlyIncome_all.setTextColor(getResources().getColor(R.color.income_green));

                    monthlyIncome_credit.setText("$"+String.valueOf(df.format(income_month_credit)));
                    monthlyIncome_credit.setTextColor(getResources().getColor(R.color.credit_cash_orange));

                    monthlyIncome_cash.setText("$"+String.valueOf(df.format(income_month_cash)));
                    monthlyIncome_cash.setTextColor(getResources().getColor(R.color.credit_cash_orange));


                }
                else{
                    Log.d("Main Page", "Error: " + e.getMessage());
                }
            }

        });


        viewExpenseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent getlist = new Intent(ctxt, CashList.class);
                getlist.putExtra("type", "expense_all");
                startActivity(getlist);
            }
        });

        viewIncomeButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent getlist = new Intent(ctxt, CashList.class);
                getlist.putExtra("type", "income_all");
                startActivity(getlist);
            }
        });

    }

    @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_main, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}




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