- @TargetApi(Build.VERSION_CODES.N)
- public static void hookAutocompletionAdapter(final Context context,
- final AutoCompleteTextView view,
- final String table, final String field,
- final boolean profileSpecific) {
- hookAutocompletionAdapter(context, view, table, field, profileSpecific, null, null,
- Data.profile.getValue());
- }
- @TargetApi(Build.VERSION_CODES.N)
- public static void hookAutocompletionAdapter(final Context context,
- final AutoCompleteTextView view,
- final String table, final String field,
- final boolean profileSpecific, final View nextView,
- final DescriptionSelectedCallback callback,
- final MobileLedgerProfile profile) {
- String[] from = {field};
- int[] to = {android.R.id.text1};
- SimpleCursorAdapter adapter =
- new SimpleCursorAdapter(context, android.R.layout.simple_dropdown_item_1line, null,
- from, to, 0);
- adapter.setStringConversionColumn(1);
-
- FilterQueryProvider provider = constraint -> {
- if (constraint == null) return null;
-
- String str = constraint.toString().toUpperCase();
- debug("autocompletion", "Looking for " + str);
- String[] col_names = {FontsContract.Columns._ID, field};
- MatrixCursor c = new MatrixCursor(col_names);
-
- String sql;
- String[] params;
- if (profileSpecific) {
- sql = String.format("SELECT %s as a, case when %s_upper LIKE ?||'%%' then 1 " +
- "WHEN %s_upper LIKE '%%:'||?||'%%' then 2 " +
- "WHEN %s_upper LIKE '%% '||?||'%%' then 3 else 9 end " +
- "FROM %s " +
- "WHERE profile=? AND %s_upper LIKE '%%'||?||'%%' " +
- "ORDER BY 2, 1;", field, field, field, field, table, field);
- params = new String[]{str, str, str, profile.getUuid(), str};
+ public static void queryInBackground(@NonNull String statement, String[] params,
+ @NonNull final 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();
+ }
+ }