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;
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;
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);
}
}
});
}
+ @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);
acc.requestFocus();
hook_swipe_listener(row);
+ hook_autocompletion_adapter(row);
}
}