私は最初のAndroidアプリケーションに取り組んでいますが、db-connectivityを実行するために多くのコードスニペットを再利用しているという問題が発生しました。同じ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;
}