作为一种非常实用的应用,记账app已经成为现代生活中不可或缺的一部分。随着拥有智能手机的用户数量不断增长,记账app已经遍地开花,成为最受欢迎的应用之一。本文将介绍如何开发一款Android记账应用程序。
在开始介绍之前,需要了解一些基础知识。在开发记账app之前,您需要熟悉Java语言和Android开发。此外,您还需要了解一些数据存储基础知识,例如SQLite数据库。
首先,我们需要设计应用的界面。一款典型的记账应用应该至少包含三个主要的界面:
1. 记录支出或收入的界面
2. 显示支出或收入的列表界面
3. 统计支出或收入的界面
在设计界面的过程中,需要注意以下事项:
1. 使用Material Design风格,使得界面实现时更加美观和易于使用。
2. 充分利用现成的布局资源如LinearLayout和RelativeLayout,避免在布局方面浪费时间和精力。
接下来是数据存储问题。为了存储支出或收入的信息,我们需要使用SQLite数据库。SQLite数据库是一种轻量级的关系型数据库,非常适合Android应用程序。通过安卓提供的SQLiteOpenHelper方法,我们可以基于SQLite库创建表格并执行数据操作(比如插入,更新和删除数据)。
最后是部分核心代码展示:
1. 创建支出或收入类型表:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "wallet.db";
private static final int DATABASE_VERSION = 1;
private static final String EXPENSE_TYPE_TABLE = "expense_types";
private static final String INCOME_TYPE_TABLE = "income_types";
private static final String COLUMN_ID = "id";
private static final String COLUMN_TITLE = "title";
private static final String CREATE_EXPENSE_TYPE_TABLE = "CREATE TABLE "
+ EXPENSE_TYPE_TABLE + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_TITLE + " TEXT NOT NULL UNIQUE"
+ ")";
private static final String CREATE_INCOME_TYPE_TABLE = "CREATE TABLE "
+ INCOME_TYPE_TABLE + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_TITLE + " TEXT NOT NULL UNIQUE"
+ ")";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_EXPENSE_TYPE_TABLE);
db.execSQL(CREATE_INCOME_TYPE_TABLE);
}
}
```
2. 创建支出或收入详细信息表:
```java
private static final String EXPENSES_TABLE = "expenses";
private static final String INCOMES_TABLE = "incomes";
private static final String COLUMN_AMOUNT = "amount";
private static final String COLUMN_TYPE_ID = "type_id";
private static final String COLUMN_NOTE = "note";
private static final String COLUMN_CREATED_AT = "created_at";
private static final String CREATE_EXPENSES_TABLE = "CREATE TABLE "
+ EXPENSES_TABLE + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_AMOUNT + " REAL NOT NULL,"
+ COLUMN_TYPE_ID + " INTEGER NOT NULL,"
+ COLUMN_NOTE + " TEXT,"
+ COLUMN_CREATED_AT + " DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "FOREIGN KEY(" + COLUMN_TYPE_ID + ") REFERENCES "
+ EXPENSE_TYPE_TABLE + "(" + COLUMN_ID + ")"
+ ")";
private static final String CREATE_INCOMES_TABLE = "CREATE TABLE "
+ INCOMES_TABLE + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_AMOUNT + " REAL NOT NULL,"
+ COLUMN_TYPE_ID + " INTEGER NOT NULL,"
+ COLUMN_NOTE + " TEXT,"
+ COLUMN_CREATED_AT + " DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "FOREIGN KEY(" + COLUMN_TYPE_ID + ") REFERENCES "
+ INCOME_TYPE_TABLE + "(" + COLUMN_ID + ")"
+ ")";
```
3. 插入支出或收入详细信息到数据表:
```java
public long addExpense(float amount, ExpenseType type, String note) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_AMOUNT, amount);
values.put(COLUMN_TYPE_ID, type.getId());
values.put(COLUMN_NOTE, note);
return db.insert(EXPENSES_TABLE, null, values);
}
public long addIncome(float amount, IncomeType type, String note) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_AMOUNT, amount);
values.put(COLUMN_TYPE_ID, type.getId());
values.put(COLUMN_NOTE, note);
return db.insert(INCOMES_TABLE, null, values);
}
```
4. 查询支出或收入详细信息:
```java
public ArrayList
ArrayList
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT expenses.*, expense_types.title FROM expenses INNER JOIN expense_types ON expenses.type_id = expense_types.id ORDER BY expenses.created_at DESC", null);
if (cursor.moveToFirst()) {
do {
ExpenseType type = new ExpenseType(
cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE_ID)),
cursor.getString(cursor.getColumnIndex("title"))
);
Expense expense = new Expense(
cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),
cursor.getFloat(cursor.getColumnIndex(COLUMN_AMOUNT)),
type,
cursor.getString(cursor.getColumnIndex(COLUMN_NOTE)),
cursor.getString(cursor.getColumnIndex(COLUMN_CREATED_AT))
);
expenses.add(expense);
} while (cursor.moveToNext());
}
cursor.close();
return expenses;
}
public ArrayList
ArrayList
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT incomes.*, income_types.title FROM incomes INNER JOIN income_types ON incomes.type_id = income_types.id ORDER BY incomes.created_at DESC", null);
if (cursor.moveToFirst()) {
do {
IncomeType type = new IncomeType(
cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE_ID)),
cursor.getString(cursor.getColumnIndex("title"))
);
Income income = new Income(
cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),
cursor.getFloat(cursor.getColumnIndex(COLUMN_AMOUNT)),
type,
cursor.getString(cursor.getColumnIndex(COLUMN_NOTE)),
cursor.getString(cursor.getColumnIndex(COLUMN_CREATED_AT))
);
incomes.add(income);
} while (cursor.moveToNext());
}
cursor.close();
return incomes;
}
```
到此为止,我们已经完成了一款基本的Android记账应用程序。在这个项目中, 最重要的部分是学习如何使用 SQLiteDatabase 去连接数据库, 并根据输入来进行数据的存入和查询。这样, 我们就可以创建存储用户在应用程序中输入数据的数据库。同时, 数据库也为我们提供了随时查看和编辑信息的控制权。