Last active
January 4, 2017 13:24
-
-
Save gugacavalieri/3c5571493a77ec7be0ffbaaa38e20bf8 to your computer and use it in GitHub Desktop.
Android AutoComplete Custom Adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Color; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.Filter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by gustavo on 1/4/17. | |
* | |
* Custom adapter for using with autocomplete view. | |
* This adapter serves for filtering colors | |
*/ | |
public class AutoCompleteColorAdapter extends ArrayAdapter<FirebaseColor> { | |
private LayoutInflater layoutInflater; | |
private List<FirebaseColor> firebaseColors; | |
private Filter mFilter = new Filter() { | |
@Override | |
public String convertResultToString(Object resultValue) { | |
return ((FirebaseColor)resultValue).getName(); | |
} | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
FilterResults results = new FilterResults(); | |
if (constraint != null) { | |
ArrayList<FirebaseColor> suggestions = new ArrayList<>(); | |
for (FirebaseColor color : firebaseColors) { | |
// Note: change the "contains" to "startsWith" if you only want starting matches | |
if (color.getName().toLowerCase().contains(constraint.toString().toLowerCase())) { | |
suggestions.add(color); | |
} | |
} | |
results.values = suggestions; | |
results.count = suggestions.size(); | |
} | |
return results; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
clear(); | |
if (results != null && results.count > 0) { | |
// we have filtered results | |
addAll((ArrayList<FirebaseColor>) results.values); | |
} else { | |
// no filter, add entire original list back in | |
addAll(firebaseColors); | |
} | |
notifyDataSetChanged(); | |
} | |
}; | |
public AutoCompleteColorAdapter(Context context, int textViewResourceId, List<FirebaseColor> colors) { | |
super(context, textViewResourceId, colors); | |
// copy all the customers into a master list | |
firebaseColors = new ArrayList<>(colors.size()); | |
firebaseColors.addAll(colors); | |
layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
@NonNull | |
@Override | |
public View getView(int position, View convertView, @NonNull ViewGroup parent) { | |
View view = convertView; | |
if (view == null) { | |
view = layoutInflater.inflate(R.layout.autocomplete_color, null); | |
} | |
FirebaseColor color = getItem(position); | |
TextView name = (TextView) view.findViewById(R.id.autoCompleteColorName); | |
ImageView colorHexa = (ImageView) view.findViewById(R.id.autocompleteColorHexa); | |
assert color != null; | |
Log.d("AutoCompleteColorAdapter", color.toString()); | |
name.setText(color.getName()); | |
colorHexa.setColorFilter(Color.parseColor(color.getColorHexa())); | |
return view; | |
} | |
@NonNull | |
@Override | |
public Filter getFilter() { | |
return mFilter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment