Java tutorial
/* * Copyright (C) 2012 Davy Leggieri * * 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 2 * 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.bydavy.card.receipts; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.FilterQueryProvider; import android.widget.Filterable; import android.widget.TextView; import com.bydavy.card.receipts.providers.Shops; import com.bydavy.card.receipts.services.SuggestionService; public class ShopSuggestionAdapter extends CursorAdapter implements Filterable { private static final String[] PROJECTION = new String[] { Shops._ID, Shops.COLUMN_NAME_NAME }; private static final int COLUMN_TO_DISPLAY_INDEX = 1; private static final String SORT_ORDER_LATEST_USE = Shops.COLUMN_NAME_COUNT + " DESC"; private static final String SELECTION; static { final StringBuilder buffer = new StringBuilder("SELECT "); buffer.append(Shops.COLUMN_NAME_NAME); buffer.append(" LIKE '%' || ? || '%'"); SELECTION = buffer.toString(); } private final Context mContext; private final AutoCompleteTextView mAutoCompleteTextView; @SuppressWarnings("deprecation") public ShopSuggestionAdapter(Context context, AutoCompleteTextView autoCompleteTextView) { super(context, null); mContext = context; mAutoCompleteTextView = autoCompleteTextView; setFilterQueryProvider(new ShopNameFilterQueryProvider()); } @Override public void bindView(View view, Context context, Cursor cursor) { final String shop = cursor.getString(COLUMN_TO_DISPLAY_INDEX); /* * ViewHolder holder = (ViewHolder) view.getTag(); holder.shop = shop; * holder.shopTV.setText(shop); */ ((TextView) view).setText(shop); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context); /* * final View view = inflater.inflate( R.layout.dropdown_cancelable, * parent, false); * * ViewHolder holder = new ViewHolder(); holder.shopTV = (TextView) * view.findViewById(R.id.shop); holder.blacklistButton = (Button) * view.findViewById(R.id.blacklist); * holder.blacklistButton.setOnClickListener(new OnClickListener() { * public void onClick(View v) { ViewHolder holder = (ViewHolder) * view.getTag(); blacklist(holder.shop); * mAutoCompleteTextView.dismissDropDown(); } }); view.setTag(holder); */ final View view = inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false); return view; } private static class ViewHolder { public String shop; public TextView shopTV; public Button blacklistButton; } @Override public String convertToString(Cursor cursor) { return cursor.getString(COLUMN_TO_DISPLAY_INDEX); } private void blacklist(String shop) { final Intent intent = new Intent(mContext, SuggestionService.class); intent.putExtra(SuggestionService.INTENT_ACTION, SuggestionService.ACTION_BLACKLIST); intent.putExtra(SuggestionService.INTENT_SHOP_NAME, shop); mContext.startService(intent); } private class ShopNameFilterQueryProvider implements FilterQueryProvider { /* Avoid GC and reuse this array */ private final String[] mSelectionArgs = new String[1]; @Override public Cursor runQuery(CharSequence constraint) { if (constraint != null) { mSelectionArgs[0] = constraint.toString(); return mContext.getContentResolver().query(Shops.CONTENT_URI, PROJECTION, SELECTION, mSelectionArgs, SORT_ORDER_LATEST_USE); } return null; } } }