Last active
October 17, 2019 13:23
-
-
Save nknr/b26409b72d6f3bfebbd9ee6385ef9840 to your computer and use it in GitHub Desktop.
CustomAutoCompleteTextView
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
public class CustomAutoCompleteAdapter extends ArrayAdapter<CustomModel> implements Filterable { | |
private List<CustomModel> fullList; | |
private List<CustomModel> mOriginalValues; | |
private ArrayFilter mFilter; | |
public CustomAutoCompleteAdapter(Context context, int resource, List<CustomModel> objects) { | |
super(context, resource, objects); | |
fullList = objects; | |
mOriginalValues = new ArrayList<>(fullList); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public int getCount() { | |
return fullList.size(); | |
} | |
@Override | |
public CustomModel getItem(int position) { | |
return fullList.get(position); | |
} | |
@NonNull | |
@Override | |
public View getView(int position, View convertView, @NonNull ViewGroup parent) { | |
TextView label = (TextView) super.getView(position, convertView, parent); | |
label.setTextColor(Color.BLACK); | |
if (fullList.get(position) != null) { | |
CustomModel model = fullList.get(position); | |
String value = model.getName()+" - "+model.getSpecification()+" - "+model.getMobile(); | |
label.setText(value); | |
} | |
return label; | |
} | |
@Override | |
public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) { | |
TextView label = (TextView) super.getView(position, convertView, parent); | |
label.setTextColor(Color.BLACK); | |
if (fullList.get(position) != null) { | |
CustomModel model = fullList.get(position); | |
String value = model.getName()+" - "+model.getSpecification()+" - "+model.getMobile(); | |
label.setText(value); | |
} | |
return label; | |
} | |
@NonNull | |
@Override | |
public Filter getFilter() { | |
if (mFilter == null) { | |
mFilter = new ArrayFilter(); | |
} | |
return mFilter; | |
} | |
private class ArrayFilter extends Filter { | |
//private Object lock; | |
@Override | |
protected FilterResults performFiltering(CharSequence prefix) { | |
FilterResults results = new FilterResults(); | |
if (mOriginalValues == null) { | |
/*synchronized (lock) {*/ | |
mOriginalValues = new ArrayList<>(fullList); | |
//} | |
} | |
if (prefix == null || prefix.length() == 0) { | |
/*synchronized (lock) {*/ | |
List<CustomModel> list = new ArrayList<>(mOriginalValues); | |
results.values = list; | |
results.count = list.size(); | |
//} | |
} else { | |
final String prefixString = prefix.toString().replaceAll("[^A-Za-z0-9]","").toLowerCase(); | |
List<CustomModel> values = mOriginalValues; | |
int count = values.size(); | |
List<CustomModel> newValues = new ArrayList<>(count); | |
for (int i = 0; i < count; i++) { | |
CustomModel item = values.get(i); | |
String itemConvert = item.getName().replaceAll("[^A-Za-z0-9]","").toLowerCase(); | |
if (itemConvert.contains(prefixString)) { | |
newValues.add(item); | |
} | |
} | |
results.values = newValues; | |
results.count = newValues.size(); | |
} | |
return results; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
if (results.values != null) { | |
fullList = (ArrayList<CustomModel>) results.values; | |
} else { | |
fullList = new ArrayList<>(); | |
} | |
if (results.count > 0) { | |
notifyDataSetChanged(); | |
} else { | |
notifyDataSetInvalidated(); | |
} | |
} | |
} | |
} |
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
public class CustomModel{ | |
// {"value":"8208","label":" ","Mobile":"0000000000","Specialization":"COORDINATOR" | |
@SerializedName("value") | |
private String id; | |
@SerializedName("label") | |
private String name; | |
@SerializedName("Mobile") | |
private String mobile; | |
@SerializedName("Specialization") | |
private String specification; | |
public CustomModel(String id, String name, String mobile, String specification) { | |
this.id = id; | |
this.name = name; | |
this.mobile = mobile; | |
this.specification = specification; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getMobile() { | |
return mobile; | |
} | |
public String getSpecification() { | |
return specification; | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="@dimen/normal_padding" | |
android:maxLines="2" | |
android:textAppearance="@style/TextAppearance.AppCompat.Subhead" | |
android:textColor="@color/grey_90" | |
android:textSize="@dimen/textSize_15" | |
tools:text="@tools:sample/cities" /> |
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
CustomAutoCompleteAdapter adapter = new CustomAutoCompleteAdapter(activity, R.layout.item_auto_complete, doctorList); | |
autoCompleteView.setAdapter(adapter); | |
autoCompleteView.setOnItemClickListener((adapterView, view, pos, id) -> { | |
CustomModel item = (CustomModel) adapterView.getItemAtPosition(pos); | |
autoCompleteView.setText(item.getName()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment