Custom ArrayAdapter for Spinner,AutoComplete TextView

January 04, 2017 0 Comments

Instruction :

SearchModel is Custom Model Which are used in the Spinner

searchTxt is Autocomplete Textview object.

Make a CustomArrayAdapter For Autocomplete TextView .
public class SearchAdapter extends ArrayAdapter<SearchModel> {

Context context;
int resource, textViewResourceId;
List<SearchModel> items, tempItems, suggestions; //Searchmodel is a custom model class

public SearchAdapter(Context context, List<SearchModel> items) {
super(context, 0, items);
this.context = context;
this.items = items;
tempItems = new ArrayList<SearchModel>(items); // this makes the difference.
suggestions = new ArrayList<SearchModel>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); //Set your Custom Layout
}
final SearchModel searchdata = items.get(position);
if (searchdata != null) {
//set text Whatever you want from your custom model
TextView lblName = (TextView) view.findViewById(android.R.id.text1);
if (lblName != null)
lblName.setText(searchdata.getEdition());
}
return view;
}

@Override
public Filter getFilter() {
return nameFilter;
}

/**
* Custom Filter implementation for custom suggestions we provide.
*/
Filter nameFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
String str = ((SearchModel) resultValue).getEdition();
return str;
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
for (SearchModel people : tempItems) {
if (people.getEdition().toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(people);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List<SearchModel> filterList = (ArrayList<SearchModel>) results.values;
if (results != null && results.count > 0) {
clear();
for (SearchModel people : filterList) {
add(people);
notifyDataSetChanged();
}
}
}
};
}


searchTxt.setAdapter(new SearchAdapter(MasterActivity.this,List list));

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *