From 5c608c57d46b9fd2e77b4a4c65800662f7c56aff Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 2 Dec 2018 10:58:43 +0000 Subject: [PATCH] dynamic auto-completion of account names, fancy!!! --- .../mobileledger/NewTransactionActivity.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java index a35c472c..fe2a14b2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java @@ -1,12 +1,19 @@ package net.ktnx.mobileledger; +import android.annotation.TargetApi; +import android.database.Cursor; +import android.database.MatrixCursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Build; import android.os.Bundle; +import android.provider.FontsContract; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.text.InputType; +import android.util.Log; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; @@ -14,6 +21,8 @@ import android.view.MotionEvent; import android.view.View; import android.widget.AutoCompleteTextView; import android.widget.EditText; +import android.widget.FilterQueryProvider; +import android.widget.SimpleCursorAdapter; import android.widget.TableLayout; import android.widget.TableRow; @@ -41,6 +50,7 @@ public class NewTransactionActivity extends AppCompatActivity { table = findViewById(R.id.new_transaction_accounts_table); for (int i = 0; i < table.getChildCount(); i++) { hook_swipe_listener((TableRow)table.getChildAt(i)); + hook_autocompletion_adapter((TableRow)table.getChildAt(i)); // Log.d("swipe", "hooked to row "+i); } } @@ -68,6 +78,50 @@ public class NewTransactionActivity extends AppCompatActivity { }); } + @TargetApi(Build.VERSION_CODES.N) + private void hook_autocompletion_adapter(final TableRow row) { + String[] from = {"name"}; + int[] to = {android.R.id.text1}; + SQLiteDatabase db = MobileLedgerDB.db; + + AutoCompleteTextView acc = (AutoCompleteTextView) row.getChildAt(0); + SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0); + adapter.setStringConversionColumn(1); + + FilterQueryProvider provider = new FilterQueryProvider() { + @Override + public Cursor runQuery(CharSequence constraint) { + if (constraint == null) return null; + + String str = constraint.toString().toUpperCase(); + Log.d("autocompletion", "Looking for "+str); + String[] col_names = {FontsContract.Columns._ID, "name"}; + MatrixCursor c = new MatrixCursor(col_names); + + Cursor matches = db.rawQuery("SELECT name FROM accounts WHERE UPPER(name) LIKE '%'||?||'%' ORDER BY name;", new String[]{str}); + + try { + int i = 0; + while (matches.moveToNext()) { + String name = matches.getString(0); + Log.d("autocompletion-match", name); + c.newRow().add(i++).add(name); + } + } + finally { + matches.close(); + } + + return c; + + } + }; + + adapter.setFilterQueryProvider(provider); + + acc.setAdapter(adapter); + } + public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.new_transaction, menu); @@ -108,6 +162,7 @@ public class NewTransactionActivity extends AppCompatActivity { acc.requestFocus(); hook_swipe_listener(row); + hook_autocompletion_adapter(row); } } -- 2.39.5