我正在开发我的第一个 Android 应用程序,我遇到了一个问题,我正在重复使用大量代码 fragment 来执行数据库连接。我看到自己一遍又一遍地重写相同的 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;
}
最佳答案
首先打开数据库时,我最好使用这样的东西:
if (db == null || !db.isOpen()) {
db = getWritableDatabase(); //getReadableDatabase();
}
它负责创建或打开数据库,以防尚未完成。
对于第一个方法 block ,我显然会采用类似的方法:
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 方法中,该方法将执行所有调用的其余逻辑。 我注意到有一种方法只返回一个对象而不是数组。在这种情况下,您可以只传递一个内部只有一个对象的数组。
希望对您有所帮助。
让我知道您的印象/更新。
关于java - 有没有一种有效的方法来重构这个 Android SQLite 数据库处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924577/