我正在开发我的第一个 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;
}