samedi 7 novembre 2015

Sqlite inserting doesn't work properly

First time writing Android app here.

My use case is following: in one activity I have a TextView that displays a summary (sum of amounts), and a Button with EditText. The idea is that pressing the button adds an entry to sqlite database.

The problem is, every time I press the button, it seems that the previous value from the input is being added to the database.

  1. Input 10 -> press button -> no reaction
  2. Input 7 -> press button -> summary changes by 10
  3. Input 3 -> press button -> summary changes by 7 etc.

Here's the method used in the 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)));
    }

Here's the log statements from that sequence of inputs:

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), 

And here's the code from the activity that handles the flow of events:

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) {

                }
            }

        });
    }

Now it is clear that after db.insert returns, the row is still not where it should be.

How do I guarantee that next call to getExpenses definitely returns the new row?

Appreciate your help.

Aucun commentaire:

Enregistrer un commentaire