第一次在这里编写Android应用。
我的用例如下:在一个活动中,我有一个显示摘要(金额总和)的TextView
和一个带有Button
的EditText
。这个想法是,按下按钮会在sqlite数据库中添加一个条目。
问题是,每当我按下按钮时,似乎输入中的先前值便被添加到数据库中。
输入10->按下按钮->无反应
输入7->按下按钮->摘要更改10
输入3->按下按钮->摘要更改7
等等
这是OnClickListener中使用的方法:
public static void addExpense(Context ctx, Expense expense) {
ExpensesDbHelper dbHelper = new ExpensesDbHelper(ctx);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = expense.toContentValues();
Log.i("Daily-", "addExpense: " + values.toString());
long newRowId;
db.beginTransaction();
try {
newRowId = db.insert(
ExpensesReaderContract.ExpenseEntry.TABLE_NAME,
null,
values);
db.setTransactionSuccessful();
} finally{
db.endTransaction();
}
Log.i("Daily-", "addExpense: new id " + newRowId);
Cursor c = db.rawQuery("SELECT COUNT(*) from entry", null);
c.moveToFirst();
Log.i("Daily-", "Number of rows: " + c.getInt(0));
c.close();
Log.i("Daily-", "addExpense: Existing " + Expense.printable(getExpenses(db)));
}
这是该输入序列中的日志语句:
I/Daily-: addExpense: timestamp=2015-11-07 16:44:38 comment=Expense amount=10.0
I/Daily-: addExpense: new id 1
I/Daily-: Number of rows: 1
I/Daily-: addExpense: Existing
I/Daely-Log: 9
I/Daily-: addExpense: timestamp=2015-11-07 16:44:41 comment=Expense amount=7.0
I/Daily-: addExpense: new id 2
I/Daily-: Number of rows: 2
I/Daily-: addExpense: Existing Expense(10.000000,Expense,2015-11-07T16:44:38.000Z),
I/Daely-Log: 9
I/Daily-: addExpense: timestamp=2015-11-07 16:44:44 comment=Expense amount=3.0
I/Daily-: addExpense: new id 3
I/Daily-: Number of rows: 3
I/Daily-: addExpense: Existing Expense(7.000000,Expense,2015-11-07T16:44:41.000Z), Expense(10.000000,Expense,2015-11-07T16:44:38.000Z),
这是处理事件流的活动中的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_summary);
showAvailable();
Button addExpenseButton = (Button) findViewById(R.id.add_expense);
final EditText amountField = (EditText) findViewById(R.id.amount);
addExpenseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
float amount = Float.parseFloat(amountField.getText().toString());
addExpense(amount, "Expense");
showAvailable();
} catch (NumberFormatException e) {
}
}
});
}
现在很明显,在
db.insert
返回之后,该行仍然不在应有的位置。如何保证对getExpenses的下一次调用肯定会返回新行?
感谢您的帮助。
更新:
Expense
是一个非常简单的类:public class Expense {
public static DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private float amount;
public float getAmount() {
return amount;
}
public String getComment() {
return comment;
}
public DateTime getTimestamp() {
return timestamp;
}
private String comment;
private DateTime timestamp;
public Expense(float amount, String comment, DateTime timestamp) {
this.amount = amount;
this.comment = comment;
this.timestamp = timestamp;
}
...
public ContentValues toContentValues() {
ContentValues values = new ContentValues();
values.put(ExpensesReaderContract.ExpenseEntry.COLUMN_NAME_AMOUNT, getAmount());
values.put(ExpensesReaderContract.ExpenseEntry.COLUMN_NAME_COMMENT, getComment());
values.put(ExpensesReaderContract.ExpenseEntry.COLUMN_NAME_TIMESTAMP, iso8601Format.format(getTimestamp().toDate()));
return values;
}
}
最佳答案
您的日志表明插入确实有效-您正在获取正确的COUNT(*)
,不是吗?我会更担心Expense.printable()
和getExpenses()
中的问题,但是在发布代码时,您似乎已经删掉了该部分代码。最有可能的是,您在附近某处丢失了一行。
如果我猜到了,您正在getExpenses()
方法中执行以下操作:
if (cursor.moveToFirst()) {
while(cursor.moveToNext()) {
<stuff>
}
}
那会让你错过第一行。如果事实证明是您的问题,只需跳过
moveToFirst()
调用,或将循环更改为do-while。