- if (nextView != null) {
- view.setOnItemClickListener((parent, itemView, position, id) -> {
- nextView.requestFocus(View.FOCUS_FORWARD);
- if (callback != null) {
- callback.descriptionSelected(String.valueOf(view.getText()));
- }
- });
- }
- }
- public static synchronized void init(Application context) {
- MLDB.context = context;
- if (dbHelper != null)
- throw new IllegalStateException("It appears init() was already called");
- dbHelper = new MobileLedgerDatabase(context);
- }
- public static synchronized void done() {
- if (dbHelper != null) {
- debug("db", "Closing DB helper");
- dbHelper.close();
- dbHelper = null;
- }
- }
-}
-
-class MobileLedgerDatabase extends SQLiteOpenHelper {
- private static final String DB_NAME = "MoLe.db";
- private static final int LATEST_REVISION = 22;
- private static final String CREATE_DB_SQL = "create_db";
-
- private final Application mContext;
-
- MobileLedgerDatabase(Application context) {
- super(context, DB_NAME, null, LATEST_REVISION);
- debug("db", "creating helper instance");
- mContext = context;
- super.setWriteAheadLoggingEnabled(true);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- debug("db", "onCreate called");
- applyRevisionFile(db, CREATE_DB_SQL);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- debug("db", "onUpgrade called");
- for (int i = oldVersion + 1; i <= newVersion; i++) applyRevision(db, i);
- }
-
- private void applyRevision(SQLiteDatabase db, int rev_no) {
- String rev_file = String.format(Locale.US, "sql_%d", rev_no);
-
- applyRevisionFile(db, rev_file);
- }
- private void applyRevisionFile(SQLiteDatabase db, String rev_file) {
- final Resources rm = mContext.getResources();
- int res_id = rm.getIdentifier(rev_file, "raw", mContext.getPackageName());
- if (res_id == 0)
- throw new SQLException(String.format(Locale.US, "No resource for %s", rev_file));
- db.beginTransaction();
- try (InputStream res = rm.openRawResource(res_id)) {
- debug("db", "Applying " + rev_file);
- InputStreamReader isr = new InputStreamReader(res);
- BufferedReader reader = new BufferedReader(isr);
-
- String line;
- int line_no = 1;
- while ((line = reader.readLine()) != null) {
- if (line.startsWith("--")) {
- line_no++;
- continue;
- }
- if (line.isEmpty()) {
- line_no++;
- continue;
- }
- try {
- db.execSQL(line);
- }
- catch (Exception e) {
- throw new RuntimeException(
- String.format("Error applying %s, line %d", rev_file, line_no), e);
+ if (callback != null)
+ view.setOnItemClickListener(
+ (parent, itemView, position, id) -> callback.descriptionSelected(
+ String.valueOf(view.getText())));
+ }
+ public static void queryInBackground(@NonNull String statement, @NonNull String[] params,
+ @NonNull CallbackHelper callbackHelper) {
+ /* All callbacks are called in the new (asynchronous) thread! */
+ Thread t = new Thread(() -> {
+ callbackHelper.onStart();
+ try {
+ SQLiteDatabase db = App.getDatabase();
+
+ try (Cursor cursor = db.rawQuery(statement, params)) {
+ boolean gotRow = false;
+ while (cursor.moveToNext()) {
+ gotRow = true;
+ if (!callbackHelper.onRow(cursor))
+ break;
+ }
+ if (!gotRow) {
+ callbackHelper.onNoRows();
+ }