1

我正在开发我的第一个 Android 应用程序,我遇到了一个问题,我重用了很多代码片段来执行数据库连接。我看到自己一遍又一遍地重写相同的 try/catch 语句,但我在寻找处理它的好方法时遇到了问题。请看一下这段代码,你有没有看到我在如何重构这个方面缺少任何明显的方式?

如您所见,这些方法通常只有 1 或 2 行差异。这是一个示例(所有其他 try/catch 代码都类似):

光标 cursor = dbAdapter.fetchReceipts(timeFrom, timeTo);

Communicator.java

    public ArrayList<Receipt> getReceipts(int limit)
{
    ArrayList<Receipt> receiptList = null;
    DbAdapter dbAdapter = new DbAdapter(context);

    try
    {
        dbAdapter.open();
        Cursor cursor = dbAdapter.fetchReceipts(limit);

        if (cursor != null)
        {
            receiptList = buildReceiptList(cursor);
        }

        dbAdapter.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }
    return receiptList;
}

public ArrayList<Receipt> getReceipts(long timeFrom, long timeTo)
{
    ArrayList<Receipt> receiptList = null;
    DbAdapter dbAdapter = new DbAdapter(context);

    try
    {
        dbAdapter.open();
        Cursor cursor = dbAdapter.fetchReceipts(timeFrom, timeTo);

        if (cursor != null)
        {
            receiptList = buildReceiptList(cursor);
        }

        dbAdapter.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }
    return receiptList;
}


public Receipt getLatestReceipt()
{
    Receipt receipt = null;
    Cursor cursor = null;
    DbAdapter dbAdapter = new DbAdapter(context);

    try
    {
        dbAdapter.open();
        cursor = dbAdapter.fetchLastReceipt();

        if (cursor.getCount() > 0)
        {
            receipt = buildReceipt(cursor);
        }

        dbAdapter.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }
    return receipt;
}

public ArrayList<Receipt> searchReceipts(String query)
{
    ArrayList<Receipt> receiptList = null;
    DbAdapter dbAdapter = new DbAdapter(context);

    try
    {
        dbAdapter.open();
        Cursor cursor = dbAdapter.searchReceiptName(query);

        if (cursor != null)
        {
            receiptList = buildReceiptList(cursor);
        }

        dbAdapter.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }

    return receiptList;
}

public boolean updateReceipt(Receipt receipt)
{
    DbAdapter dbAdapter = new DbAdapter(context);
    boolean result = false;
    try
    {
        dbAdapter.open();
        result = dbAdapter.updateReceipt(receipt.getId(), receipt.getName(), receipt.getPhoto(), receipt.getTimestamp(),
                receipt.getLocationLat(), receipt.getLocationLong(), receipt.getSum(), receipt.getTax(), receipt.getComment());
        showResult(result);
        dbAdapter.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }
    return result;
}

private boolean insertReceipt(Receipt receipt)
{
    boolean result = false;
    DbAdapter dbAdapter = new DbAdapter(context);
    try
    {
        dbAdapter.open();
        result = dbAdapter.createReceipt(receipt.getName(), receipt.getPhoto(), receipt.getTimestamp(), receipt.getLocationLat(),
                receipt.getLocationLong(), receipt.getSum(), receipt.getTax(), receipt.getComment());
        showResult(result);
        dbAdapter.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }
    return result;
}

DBAdapter.java

public Cursor fetchReceipt(long rowId) throws SQLException
{

    Cursor cursor = db
            .query(true, DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP, KEY_LOCATION_LAT,
                    KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
    if (cursor != null)
    {
        cursor.moveToFirst();
    }
    return cursor;

}

public Cursor fetchReceipts(long timeFrom, long timeTo)
{
    Cursor cursor = db.query(true, DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP,
            KEY_LOCATION_LAT, KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, KEY_TIMESTAMP + ">" + timeFrom + " AND "
            + KEY_TIMESTAMP + "<" + timeTo, null, null, null, KEY_TIMESTAMP + " DESC", null);
    if (cursor != null)
    {
        cursor.moveToFirst();
    }
    return cursor;
}

public Cursor fetchLastReceipt()
{
    Cursor cursor = db.query(true, DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP,
            KEY_LOCATION_LAT, KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, null, null, null, null, KEY_ROWID + " DESC", "1");
    if (cursor != null)
    {
        cursor.moveToFirst();
    }
    return cursor;
}

public Cursor searchReceiptName(String query)
    {
        Cursor cursor = db.query(DATABASE_TABLE_RECEIPTS, new String[] { KEY_ROWID, KEY_NAME, KEY_PHOTO, KEY_TIMESTAMP, KEY_LOCATION_LAT,
                KEY_LOCATION_LONG, KEY_SUM, KEY_TAX, KEY_COMMENT }, KEY_NAME + " LIKE ?", new String[] { "%" + query + "%" }, null, null,
                null);
        if (cursor != null)
        {
            cursor.moveToFirst();
        }
        return cursor;
    }
4

1 回答 1

1

首先,在打开数据库时,我最好使用这样的东西:

if (db == null || !db.isOpen()) {
db = getWritableDatabase(); //getReadableDatabase();
}

它会照顾您创建或打开数据库,以防它尚未完成。

对于第一个方法块,我显然会采用以下方法:

public ArrayList<Receipt> searchReceipts(String query, DbAdapter _db, Cursor cursor)
{
    ArrayList<Receipt> receiptList = null;

    try
    {
        _db.open();

        if (cursor != null)
        {
            receiptList = buildReceiptList(cursor);
        }

        _db.close();
    }
    catch (SQLException e)
    {
        Log.d(context.getString(R.string.tag_receipttracker), e.getMessage());
        showToast(MESSAGE_COULD_NOT_OPEN);
    }

    return receiptList;
}

这样,您只需要预先创建适配器和游标,并将它们传递到该 main 方法中,该方法将为所有调用执行其余逻辑。我注意到有一种方法可以只返回一个对象而不是数组。在这种情况下,您可以只传递一个内部包含唯一对象的数组。

希望能帮助到你。

让我知道您的印象/更新。

于 2012-08-12T20:29:54.420 回答