Java tutorial
/* * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen * * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany] * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program; if * not, see <http://www.gnu.org/licenses/>. */ package de.aw.monma.views; import android.content.Context; import android.database.Cursor; import android.databinding.BindingAdapter; import android.support.annotation.NonNull; import android.support.v4.widget.SimpleCursorAdapter; import android.util.AttributeSet; import android.view.View; import android.widget.AdapterView; import android.widget.FilterQueryProvider; import de.aw.monma.R; import de.aw.monma.database.DBDefinition; import static de.aw.awlib.database.TableColumns._id; public class MonMaAutoCompleteTextView extends android.support.v7.widget.AppCompatAutoCompleteTextView implements FilterQueryProvider, AdapterView.OnItemClickListener { protected String mLastValidText; protected long mLastSelectedID; private boolean initializedCalled; private DBDefinition tbd; private String[] mProjection; private String mSelection; private String mOrderBy; private OnTextChangedListener mOnTextChangeListener; public MonMaAutoCompleteTextView(Context context) { super(context); doInit(); } public MonMaAutoCompleteTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); doInit(); } public MonMaAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); doInit(); } @BindingAdapter({ "onTextChanged" }) public static void onTextChanged(MonMaAutoCompleteTextView view, OnTextChangedListener listener) { view.setOnTextChangedListener(listener); } @BindingAdapter({ "startID" }) public static void setStartID(MonMaAutoCompleteTextView view, long startID) { view.setStartID(startID); } private void doInit() { if (!isInEditMode()) { setOnItemClickListener(this); setSelectAllOnFocus(true); setThreshold(0); setFocusable(true); setFocusableInTouchMode(true); setDropDownBackgroundResource(R.color.white); // setDropDownHeight(getLineHeight() * 5); } } public long getSelectionID() { return mLastSelectedID; } /** * Initialisiert AutoCompleteTextView. * * @param tbd * DBDefinition. Aus dieser Tabelle wird das Feld gelesen * @param selection * selection * @param selectionArgs * Argumente zur Selection * @param column * Feld, welches fuer die Selection benutzt werden soll. * @param orderBy * * @throws NullPointerException, * wenn LoaderManager null ist. */ public final void initialize(DBDefinition tbd, String selection, String[] selectionArgs, @NonNull String column, String orderBy) { if (!isInEditMode()) { initializedCalled = true; this.tbd = tbd; mProjection = new String[] { column, _id }; mSelection = column + " Like ? "; mOrderBy = orderBy; if (mOrderBy == null) { mOrderBy = "LENGTH(" + column + ")"; } if (selection != null) { if (selectionArgs != null) { for (String sel : selectionArgs) { selection = selection.replaceFirst("\\?", "'" + sel + "'"); } } mSelection = mSelection + " AND (" + selection + ")"; } mSelection = mSelection + " GROUP BY " + column; mOrderBy = mOrderBy + ", " + column; } } @Override protected void onFinishInflate() { super.onFinishInflate(); if (!initializedCalled && !isInEditMode()) { throw new IllegalStateException( "Method 'initialize(AWDBDefinition, String, " + "String[], int, String)' not called"); } SimpleCursorAdapter mSimpleCursorAdapter = new SimpleCursorAdapter(getContext(), android.R.layout.simple_dropdown_item_1line, null, mProjection, new int[] { android.R.id.text1 }, 0) { @Override public CharSequence convertToString(Cursor cursor) { return cursor.getString(0); } }; mSimpleCursorAdapter.setFilterQueryProvider(this); setAdapter(mSimpleCursorAdapter); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor c = (Cursor) parent.getItemAtPosition(position); c.moveToPosition(position); mLastValidText = c.getString(0); mLastSelectedID = c.getLong(1); if (mOnTextChangeListener != null) { mOnTextChangeListener.onTextChanged(this, mLastValidText, mLastSelectedID); } } @Override public Cursor runQuery(CharSequence constraint) { final String mConstraint = constraint == null ? "" : constraint.toString().trim(); String[] mSelectionArgs = new String[] { "%" + mConstraint + "%" }; Cursor c = getContext().getContentResolver().query(tbd.getUri(), mProjection, mSelection, mSelectionArgs, mOrderBy); if (c.moveToFirst()) { mLastSelectedID = c.getLong(1); mLastValidText = c.getString(0); } else { mLastSelectedID = NO_ID; } if (mOnTextChangeListener != null) { mOnTextChangeListener.onTextChanged(this, getText(), mLastSelectedID); } return c; } public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener) { mOnTextChangeListener = onTextChangedListener; } private void setStartID(long id) { String sel = _id + " = ?"; String[] selArg = new String[] { String.valueOf(id) }; Cursor c = getContext().getContentResolver().query(tbd.getUri(), mProjection, sel, selArg, null); try { if (c.moveToFirst()) { setText(c.getString(0)); } } finally { c.close(); } } /** * Interface fuer Listener auf Textaenderungen */ public interface OnTextChangedListener { /** * Wird gerufen, wenn sich der Text einer View geaendert hat * * @param view * view, deren Text sich geaendert hat * @param currentText * Neuer Text. * @param currentTextID */ void onTextChanged(View view, CharSequence currentText, long currentTextID); } }