如何显示与android一起使用的余额/收入和费用之和

时间:2015-04-10 15:08:45

标签: android database android-sqlite

我正在构建一个处理用户收入和费用的应用程序,我已经建立了数据库,根据用户操作保存并显示listview中的每个收入和费用,但现在我几乎坚持继续我的应用程序,因为我不喜欢知道如何显示总体余额/收入和支出,例如当用户添加100美元的金额并且背景需要追加以前的余额时。

我非常感谢任何示例和提示

这是我的dbhandler类``

public class ExpDbHandler extends SQLiteOpenHelper {

// columns of the expenses table
public static final String TABLE_EXPENSE = "expense";
public static final String COLUMN_EXPENSE_ID = "_id";
public static final String COLUMN_EXPENSE_AMOUNT = "amount";
public static final String COLUMN_EXPENSE_LOCATION = "location";
public static final String COLUMN_EXPENSE_PAYMENT_METHOD = "payment_method";
public static final String COLUMN_EXPENSE_CATEGORY = "category";
public static final String COLUMN_EXPENSE_DESCRIPTION = "description";
public static final String COLUMN_EXPENSE_DATE = "date";
public static final String COLUMN_EXPENSE_TIME = "time";

static final String DATABASE_NAME = "expense.db";
static final int DATABASE_VERSION = 3;

// columns of the Income table
public static final String TABLE_INCOME = "income";
public static final String COLUMN_INCOME_ID = "_id";
public static final String COLUMN_INCOME_AMOUNT = "amount";
public static final String COLUMN_INCOME_LOCATION = "location";
public static final String COLUMN_INCOME_PAYMENT_METHOD = "payment_method";
public static final String COLUMN_INCOME_CATEGORY = "category";
public static final String COLUMN_INCOME_DESCRIPTION = "description";
public static final String COLUMN_INCOME_DATE = "date";
public static final String COLUMN_INCOME_TIME = "time";

private static final String SQL_CREATE_TABLE_EXPENSE = "CREATE TABLE "
        + TABLE_EXPENSE + "("
        + COLUMN_EXPENSE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COLUMN_EXPENSE_AMOUNT + " REAL NOT NULL, "
        + COLUMN_EXPENSE_LOCATION + " TEXT, "
        + COLUMN_EXPENSE_PAYMENT_METHOD + " TEXT, "
        + COLUMN_EXPENSE_CATEGORY + " TEXT, "
        + COLUMN_EXPENSE_DESCRIPTION + " TEXT, "
        + COLUMN_EXPENSE_DATE + " TEXT, "
        + COLUMN_EXPENSE_TIME + " TEXT )";

// create table for income
private static final String SQL_CREATE_TABLE_INCOME = "CREATE TABLE "
        + TABLE_INCOME + "("
        + COLUMN_INCOME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COLUMN_INCOME_AMOUNT + " REAL NOT NULL, "
        + COLUMN_INCOME_LOCATION + " TEXT, "
        + COLUMN_INCOME_PAYMENT_METHOD + " TEXT, "
        + COLUMN_INCOME_CATEGORY + " TEXT, "
        + COLUMN_INCOME_DESCRIPTION + " TEXT, "
        + COLUMN_INCOME_DATE + " TEXT, "
        + COLUMN_INCOME_TIME + " TEXT )";

private Context context;

public ExpDbHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(SQL_CREATE_TABLE_EXPENSE);
        db.execSQL(SQL_CREATE_TABLE_INCOME);
        Message.message(context,"ITs Created Successfully");
    } catch (SQLException e) {
        Message.message(context,"not created" +e);
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    try {
        Message.message(context,
                "Upgrading the database from version " + oldVersion + " to "+ newVersion);
        // CLEAR DATA
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EXPENSE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_INCOME);
        // AND CALLBACK TO RECREATE A TABLE
        onCreate(db);
    } catch (SQLException e) {
        Message.message(context,
                "" + e);
    }

}



public ExpDbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

}

和我声明ExpenseDAO类的DataAccessObject

public class ExpenseDAO {

private ExpDbHandler mExpDbHandler;
private SQLiteDatabase mDatabase;
public static final String TAG = "CompanyDAO";
private Context mContext;
private String[] mAllColumns = {ExpDbHandler.COLUMN_EXPENSE_ID, ExpDbHandler.COLUMN_EXPENSE_AMOUNT, ExpDbHandler.COLUMN_EXPENSE_LOCATION,
        ExpDbHandler.COLUMN_EXPENSE_PAYMENT_METHOD, ExpDbHandler.COLUMN_EXPENSE_CATEGORY, ExpDbHandler.COLUMN_EXPENSE_DESCRIPTION,
        ExpDbHandler.COLUMN_EXPENSE_DATE, ExpDbHandler.COLUMN_EXPENSE_TIME};

public ExpenseDAO(Context context) {
    mExpDbHandler = new ExpDbHandler(context);
    this.mContext = context;
    // database open

    try {
        open();

    } catch (Exception e) {
        Log.e(TAG, "SQLEXCEPTION FOR OPENING DATABASE" + e.getMessage());
        e.printStackTrace();
    }
}

public void open() throws SQLException {
    mDatabase = mExpDbHandler.getWritableDatabase();

}

public void close() {
    mExpDbHandler.close();
}

public ExpenseInfo createExpenseInfo(double amount, String location, String payment_method, String category, String description,
                                     String date, String time) {

    ContentValues values = new ContentValues();
    values.put(ExpDbHandler.COLUMN_EXPENSE_AMOUNT, amount);
    values.put(ExpDbHandler.COLUMN_EXPENSE_LOCATION, location);
    values.put(ExpDbHandler.COLUMN_EXPENSE_PAYMENT_METHOD, payment_method);
    values.put(ExpDbHandler.COLUMN_EXPENSE_CATEGORY, category);
    values.put(ExpDbHandler.COLUMN_EXPENSE_DESCRIPTION, description);
    values.put(ExpDbHandler.COLUMN_EXPENSE_DATE, date);
    values.put(ExpDbHandler.COLUMN_EXPENSE_TIME, time);

    long insertID = mDatabase.insert(ExpDbHandler.TABLE_EXPENSE, null, values);
    Cursor cursor = mDatabase.query(ExpDbHandler.TABLE_EXPENSE, mAllColumns,
            ExpDbHandler.COLUMN_EXPENSE_ID + " = " + insertID, null, null, null, null);

    cursor.moveToFirst();
    ExpenseInfo NewExpenseInfo = cursorToExpenseInfo(cursor);
    cursor.close();
    return NewExpenseInfo;
}

public List<ExpenseInfo> getAllExpenses() {
    List<ExpenseInfo> listExpenses = new ArrayList<ExpenseInfo>();

    Cursor cursor = mDatabase.query(ExpDbHandler.TABLE_EXPENSE, mAllColumns,
            null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        ExpenseInfo expenseInfo = cursorToExpenseInfo(cursor);
        listExpenses.add(expenseInfo);
        cursor.moveToNext();
    }

    //  I need to make sure to close the cursor
    cursor.close();

    return listExpenses;
}

public List<ExpenseInfo> getExpenseById(long id) {
    List<ExpenseInfo> listExpenseInfo = new ArrayList<>();

    Cursor cursor = mDatabase.query(ExpDbHandler.TABLE_EXPENSE, mAllColumns,
            ExpDbHandler.COLUMN_EXPENSE_ID + " = ?",
            new String[]{String.valueOf(id)}, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        ExpenseInfo expenseInfo = cursorToExpenseInfo(cursor);
        listExpenseInfo.add(expenseInfo);
        cursor.moveToNext();
    }

    //  close cursor
    cursor.close();
    return listExpenseInfo;

}

protected ExpenseInfo cursorToExpenseInfo(Cursor cursor) {
    ExpenseInfo expenseInfo = new ExpenseInfo();
    expenseInfo.setId(cursor.getLong(0));
    expenseInfo.setAmount(cursor.getDouble(1));
    expenseInfo.setLocation(cursor.getString(2));
    expenseInfo.setPayment_method(cursor.getString(3));
    expenseInfo.setCategory(cursor.getString(4));
    expenseInfo.setDescription(cursor.getString(5));
    expenseInfo.setDate(cursor.getString(6));
    expenseInfo.setTime(cursor.getString(7));
    return expenseInfo;
}

}

我的ExpenseInfo类

public class ExpenseInfo{

private long mId;
private double mAmount;
private String mLocation;
private String mPayment_method;
private String mCategory;
private String mDescription;
private String mDate;
private String mTime;

public ExpenseInfo(){

}

public ExpenseInfo (double amount, String location, String payment_method,String category, String description, String date,
                    String time){
    this.mAmount=amount;
    this.mLocation=location;
    this.mPayment_method=payment_method;
    this.mCategory=category;
    this.mDescription=description;
    this.mDate=date;
    this.mTime=time;
}

public long getId() {
    return mId;
}

public double getAmount() {
    return mAmount;
}

public String getLocation() {
    return mLocation;
}

public String getPayment_method() {
    return mPayment_method;
}

public String getCategory() {
    return mCategory;
}

public String getDescription() {
    return mDescription;
}

public String getDate() {
    return mDate;
}

public String getTime() {
    return mTime;
}

public void setId(long mId) {
    this.mId = mId;
}

public void setAmount(double mAmount) {
    this.mAmount = mAmount;
}

public void setLocation(String mLocation) {
    this.mLocation = mLocation;
}

public void setPayment_method(String mPayment_method) {
    this.mPayment_method = mPayment_method;
}

public void setCategory(String mCategory) {
    this.mCategory = mCategory;
}

public void setDescription(String mDescription) {
    this.mDescription = mDescription;
}

public void setDate(String mDate) {
    this.mDate = mDate;
}

public void setTime(String mTime) {
    this.mTime = mTime;
}

}

1 个答案:

答案 0 :(得分:1)

Here是在列表视图中显示您可能正在使用的项目的一个很好的示例。 在列表视图中显示项目的方法有很多种,但主要部分是包含数据的列表适配器。 考虑到您的方法,您将从DAO类获取更新列表,您可以通过扩展BaseAdapter类来扩展创建自定义适配器。 Here's另一个例子。 从DAO类中获取列表后,您可以更新自定义适配器中的列表,然后调用适配器的notifyDataSetChanged()方法。

另一种方法可能是根本不使用DAO并使用ContentProviders和加载器。我发现内容提供商和加载器在开始时学习起来有点棘手,但它们使生活变得更加容易。 我在我的一个项目中使用过它们,你可以探索它hereThis是关于装载机的非常好的文章。我建议了解加载器和内容提供程序,因为它们比DAO和表到对象映射非常方便和可维护。