No results with custom ArrayAdapter Filter - android

I'm using the ArrayAdapter on a AutoCompleteTextView. results.values has the expected value but I get no list on the UI.
public class CustomArrayAdapter extends ArrayAdapter<String> implements Filterable {
private final Object mLock = new Object();
private CustomFilter mFilter;
public ArrayList<String> mItems;
public ArrayList<String> mFiltered;
public CustomArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mItems = new ArrayList<String>();
mFiltered = new ArrayList<String>();
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new CustomFilter();
}
return mFilter;
}
#Override
public void add(String s) {
mItems.add(s);
}
private class CustomFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (mItems == null) {
synchronized (mLock) {
mItems = new ArrayList<String>();
}
}
if (constraint == null || constraint.length() == 0) {
synchronized (mLock) {
results.values = mItems;
results.count = mItems.size();
}
} else {
final ArrayList<String> newItems = new ArrayList<String>();
for (int i = 0; i < mItems.size(); i++) {
final String item = mItems.get(i);
if(item.contains(constraint)) {
newItems.add(item);
}
}
results.values = newItems;
results.count = newItems.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mFiltered.clear();
mFiltered.addAll((ArrayList<String>) results.values);
notifyDataSetChanged();
}
}
}

Looks like overriding a couple more functions did the job.
Full source of working version:
public class CustomArrayAdapter extends ArrayAdapter<String> implements Filterable {
private List<String> mOrigionalValues;
private List<String> mObjects;
private Filter mFilter;
public CustomArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mOrigionalValues = new ArrayList<String>();
mObjects = new ArrayList<String>();
}
public void add(String object) {
mOrigionalValues.add(object);
this.notifyDataSetChanged();
}
#Override
public int getCount() {
return mObjects.size();
}
#Override
public String getItem(int position) {
return mObjects.get(position);
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new CustomFilter();
}
return mFilter;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
private class CustomFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint == null || constraint.length() == 0) {
ArrayList<String> list = new ArrayList<String>(mOrigionalValues);
results.values = list;
results.count = list.size();
} else {
ArrayList<String> newValues = new ArrayList<String>();
for(int i = 0; i < mOrigionalValues.size(); i++) {
String item = mOrigionalValues.get(i);
if(item.contains(constraint)) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mObjects = (List<String>) results.values;
Log.d("CustomArrayAdapter", String.valueOf(results.values));
Log.d("CustomArrayAdapter", String.valueOf(results.count));
notifyDataSetChanged();
}
}
}

You need overwrite getCount method in CustomArrayAdapter:
#Override
public int getCount() {
return mFiltered.size();
}
And modify your publishResults method code:
protected void publishResults(CharSequence constraint,FilterResults results) {
mFiltered = (List<String>) results.values;
notifyDataSetChanged();
}

There are some days I have been researching on the net about how to do research in arraylist with hashmap and luckily I came across this post.
I made my adaptation to arraylist > and it worked. And now I'm sharing with you, you will know there is someone like the same question using ArrayList and HashMap. Thank you Pim Reijersen!
public class CategoriaAdapterModel extends BaseAdapter implements Filterable {
private Activity context;
private ArrayList<HashMap<String, String>> mDataShown;
private ArrayList<HashMap<String, String>> mAllData;
private Filter mFilter;
private LayoutInflater inflater;
DownloadImagemUtil downloader;
public CategoriaAdapterModel(Activity context,
ArrayList<HashMap<String, String>> data) {
this.context = context;
this.mAllData = data;
this.mDataShown = data;
downloader = new DownloadImagemUtil(context);
}
public void add(HashMap<String, String> object) {
mAllData.add(object);
this.notifyDataSetChanged();
}
public int getCount() {
return mDataShown.size();
}
public Object getItem(int position) {
return mDataShown.get(position);
}
public long getItemId(int position) {
return position;
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new CustomFilter();
}
return mFilter;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
private class CustomFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(
mAllData);
results.values = list;
results.count = list.size();
} else {
ArrayList<HashMap<String, String>> newValues = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < mAllData.size(); i++) {
HashMap<String, String> item = mAllData.get(i);
if (item.get(JsonFragmentCategoriaCONN.TAG_NOME)
.toLowerCase()
.contains(constraint.toString().toLowerCase())) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mDataShown = (ArrayList<HashMap<String, String>>) results.values;
Log.d("CustomArrayAdapter", String.valueOf(results.values));
Log.d("CustomArrayAdapter", String.valueOf(results.count));
notifyDataSetChanged();
}
}

Related

Filter ListView from custom base adapter

I am trying to implement the search in the Custom ListView. I am able to search in my list. But the problem with my adapter is once the query string is not available in the list even if i backspace my string and write the correct string it's not able to search it. And my other question is how can I refresh the list with my old list which was present before the search.
Here is my code:
public class ChartListAdapter extends BaseAdapter implements Filterable {
ArrayList<ChartModel> list;
Context context;
public ChartListAdapter(ArrayList<ChartModel> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
if(list != null) {
return list.size();
} else {
return 0;
}
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null) {
view = View.inflate(context, R.layout.chart_card, null);
}
TextView chart_name = view.findViewById(R.id.chart_name);
SwitchCompat switchCompat = view.findViewById(R.id.chart_selected);
switchCompat.setTag(list.get(i).getChart_id());
chart_name.setText(list.get(i).getChart_name());
switchCompat.setChecked(list.get(i).getCard_selected());
switchCompat.setOnCheckedChangeListener((compoundButton, b) -> {
String getTag = compoundButton.getTag().toString();
Toast.makeText(context, getTag + " is selected :" + b, Toast.LENGTH_LONG).show();
});
return view;
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint == null || constraint.length() == 0){
filterResults.count = list.size();
filterResults.values = list;
}else{
ArrayList<ChartModel> resultsModel = new ArrayList<>();
String searchStr = constraint.toString().toLowerCase();
for(ChartModel itemsModel:list){
if(itemsModel.getChart_id().contains(searchStr)){
resultsModel.add(itemsModel);
}
filterResults.count = resultsModel.size();
filterResults.values = resultsModel;
}
}
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
list = (ArrayList<ChartModel>) results.values;
notifyDataSetChanged();
}
};
}
}
Any suggestion will be of great help. Thank you for your time.
Change your adapter class like this
public class ChartListAdapter extends BaseAdapter implements Filterable {
ArrayList<ChartModel> list;
ArrayList<ChartModel> filteredList;
Context context;
public ChartListAdapter(ArrayList<ChartModel> list, Context context) {
this.list = list;
this.filteredList = list;
this.context = context;
}
#Override
public int getCount() {
if(filteredList != null) {
return filteredList.size();
} else {
return 0;
}
}
#Override
public Object getItem(int i) {
return filteredList.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null) {
view = View.inflate(context, R.layout.chart_card, null);
}
TextView chart_name = view.findViewById(R.id.chart_name);
SwitchCompat switchCompat = view.findViewById(R.id.chart_selected);
switchCompat.setTag(filteredList.get(i).getChart_id());
chart_name.setText(filteredList.get(i).getChart_name());
switchCompat.setChecked(filteredList.get(i).getCard_selected());
switchCompat.setOnCheckedChangeListener((compoundButton, b) -> {
String getTag = compoundButton.getTag().toString();
Toast.makeText(context, getTag + " is selected :" + b, Toast.LENGTH_LONG).show();
});
return view;
}
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint == null || constraint.length() == 0){
filteredList = list;
}else{
ArrayList<ChartModel> resultsModel = new ArrayList<>();
String searchStr = constraint.toString().toLowerCase();
for(ChartModel itemsModel:list){
if(itemsModel.getChart_id().contains(searchStr)){
resultsModel.add(itemsModel);
}
filteredList = resultsModel;
}
}
filterResults.values = filteredList;
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<ChartModel>) results.values;
notifyDataSetChanged();
}
};
}
}

Android Listview search filter (list empty after first search)

I am doing a search filter using filterable. I manage to get the search result when I search by keyword but the listview is empty after the first search. I want it to show all the data when the user input is null.
This is the code I edited. Now I cannot get any search result. Any idea which part still wrong?
public class ProductListAdapter extends BaseAdapter implements Filterable {
private Context context;
private int layout;
private ArrayList<Booth> productList= new ArrayList<>();
private ArrayList<Booth> tempList = new ArrayList<>();
private ValueFilter mFilter = new ValueFilter();
public ProductListAdapter(Context context, int layout, ArrayList<Booth> productList) {
this.context = context;
this.layout = layout;
this.productList = productList;
this.tempList = productList;
}
#Override
public int getCount() {
return tempList.size();
}
public void addItems(ArrayList<Booth> items) {
productList.addAll(items);
tempList.addAll(items);
notifyDataSetChanged();
}
#Override
public Object getItem(int position) {
return tempList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(
int position, View view, ViewGroup viewGroup) {
Typeface face_02 = Typeface.createFromAsset(context.getAssets(), "customfont/grb.otf");
ViewHolder holder = new ViewHolder();
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layout, null);
holder.Boothname = (TextView) view.findViewById(R.id.Boothname);
holder.Rating = (TextView) view.findViewById(R.id.Rating);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Booth product = productList.get(position);
holder.Boothname.setText(product.getBoothName());
holder.Rating.setText(product.getRating());
holder.Rating.setTypeface(face_02);
holder.Boothname.setTypeface(face_02);
return view;
}
#Override
public Filter getFilter() {
return mFilter;
}
private class ValueFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Booth> filterList = new ArrayList<Booth>();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < productList.size(); i++) {
if ((productList.get(i).getBoothName().toLowerCase())
.contains(constraint.toString().toLowerCase())) {
Booth boothdata = new Booth(productList.get(i)
.getBoothName(), productList.get(i)
.getRating());
filterList.add(boothdata);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = productList.size();
results.values = productList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
tempList = (ArrayList<Booth>) results.values;
notifyDataSetChanged();
}
}
class ViewHolder {
TextView Boothname, Rating;
}
}
This is happened because you are updating your original list when user search anything.You have to use tempList which is hold temporary data and used to show search result,it is also used to shows list initially.And ProductList contains original list and it is used to compare with the search string.
Initialize variable
private List<Booth> productList=new ArrayList<>(); //you have already done this,this contains original list
private List<Booth> tempList=new ArrayList<>(); //add this one is to show search result
Method for the add data should be like this:
public void addItems(List<Booth> items) {
productList.addAll(items);
tempList.addAll(items);
notifyDataSetChanged();
}
Method for the remove data should be like this:
public void removeItems(){
productList.clear();
tempList.clear();
}
getItem and getCount method should be like this:
#Override
public int getCount() {
return tempList.size();
}
#Override
public Booth getItem(int position) {
return tempList.get(position);
}
ValueFilter should be like this:
private class ValueFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
//filter list as a local variable
ArrayList<Booth> filterList = new ArrayList<Booth>();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < productList.size(); i++) {
if ((productList.get(i).getBoothName().toLowerCase())
.startsWith(constraint.toString().toLowerCase())) {
Booth boothdata = new Booth(productList.get(i)
.getBoothName(), productList .get(i)
.getRating());
filterList.add(boothdata);
}
}
results.count = filterList.size();
results.values = filterList;
Log.e("VALUES", results.values.toString());
} else {
results.count = productList.size();
results.values = productList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
tempList = (ArrayList<Booth>) results.values;
notifyDataSetChanged();
}
}
yes, you got this error. Why? Because after searchings, from times to times, your productList loses its product. So, how to resolve it? You should make filter data for filtering only and you will search in the filter data instead of your productList as below:
filterList = new List<Product>()// do clone here when you set new data to your list.
// then in the performFiltering(), use filterList instead.
for (int i = 0; i < filterList.size(); i++) {
if ((filterList.get(i).getBoothName().toLowerCase())
.startsWith(constraint.toString().toLowerCase())) {
Booth boothdata = new Booth(filterList.get(i)
.getBoothName(), filterList.get(i)
.getRating());
filterList.add(boothdata);
}
}
This is what you should do.
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
productList = (ArrayList<Booth>) results.values; // if you use templist here, there's no change in getView
notifyDataSetChanged();
}

TextWatcher that changes two ListViews with two custom ArrayAdapters

I have the code for TextWatcher that changes 2 different listViews using 2 different ArrayAdapters.
private TextWatcher createAndReturnTextWatcher()
{
final List<String> list = dbExtractor.getDataForSearchAdapters();
adapterForSearch = new MyAdapter(this, R.layout.list_item2, R.id.product_name,
list);
adapter = new ArrayAdapter(this, R.layout.list_item2, R.id.product_name,
dbExtractor.getDataForSearchAdapters());
TextWatcher watcher1;
watcher1 = new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
if (cs.length() == 0) {
lv.setAdapter(null);
lv2.setAdapter(null);
dc.clickedOnce = true;
dc.decrement();
dc.checkDimCounter();
} else {
lv.setAdapter(adapterForSearch);
adapterForSearch.getFilter().filter(cs);
lv2.setAdapter(adapter);
adapter.getFilter().filter(cs);
if (dc.clickedOnce) {
dc.increment();
dc.checkDimCounter();
dc.clickedOnce = false;
}
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
};
return watcher1;
}
The question is:
Why this works properly when I use one standart ArrayAdapter class, and one custom ArrayAdapter class, but it doesn't work if I try to use both custom ArrayAdapters. This just changes both listViews like they both use one of custom adapters?
Thanks in advance!
MY CUSTOM ADAPTERS
1st
public class MyTurkishAdapter extends ArrayAdapter {
protected Filter myTurkFilter;
protected List<String> mObjects;
protected List<String> mOriginalValues;
public MyTurkishAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
myTurkFilter = new MyTurkFilter();
mObjects = objects;
}
#Override
public Filter getFilter() {
return myTurkFilter;
}
private class MyTurkFilter extends Filter
{
#Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
{
mOriginalValues = new ArrayList<String>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<String> list;
{
list = new ArrayList<>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
java.lang.String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values;
{
values = new ArrayList<>(mOriginalValues);
}
final int count = values.size();
final ArrayList<String> newValues = new ArrayList<String>();
for (int i = 0; i < count; i++) {
final String value = values.get(i);
final java.lang.String valueText = value.toLowerCase();
if (valueText.startsWith(prefixString)) {
newValues.add(reverse(value));
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mObjects.clear();
mObjects.addAll((List<String>) results.values);
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
private java.lang.String reverse(java.lang.String couples)
{
java.lang.String[] arr = couples.split(" — ");
return arr[1]+ " — " + arr[0];
}
}
2nd
public class MyAdapter extends ArrayAdapter {
protected Filter filter;
protected List<String> mObjects;
protected List<String> mOriginalValues;
public MyAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
filter = new MyFilter();
mObjects = objects;
}
#Override
public Filter getFilter() {
return filter;
}
private class MyFilter extends Filter
{
#Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
{
mOriginalValues = new ArrayList<String>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
ArrayList<String> list;
{
list = new ArrayList<String>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
java.lang.String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values;
{
values = new ArrayList<String>(mOriginalValues);
}
final int count = values.size();
final ArrayList<String> newValues = new ArrayList<String>();
//DictionaryActivity2.turkish.clear();
for (int i = 0; i < count; i++) {
final String value = values.get(i);
final java.lang.String valueText = value.toString().toLowerCase();
if (valueText.startsWith(prefixString)) {
newValues.add(value);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mObjects.clear();
mObjects.addAll((List<String>) results.values);
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
P.S. They have just one line difference. I know about Template Method pattern and will implement this right after I'll find the answer for this question :)

Filter Adapter not displaying the filter results Android

My listview will not filter the results on the activity. when i type the
the vales get filtered successfully in my Adapter. i checked it by running the log in the below code. in the log it displays the filtered values.
But i dont see any changes in the activity
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
listData = (ArrayList<ContactLists>) results.values;
Log.e("FILTERED: ",results.values.toString()); // filter results appear here
notifyDataSetChanged();
}
but my activity does not filter the listview. my activity code below
ContactListAdapter myAdapter;
ArrayList<ContactLists> listData;
...
...
public void getContacts(){
listData = getListData();
myAdapter = new ContactListAdapter(this, listData);
listView = (ListView) findViewById(R.id.custom_list);
listView.setAdapter(new ContactListAdapter(this, listData));
}
public boolean onQueryTextChange(String newText) {
myAdapter.getFilter().filter(newText);
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
CustomAdapter
ArrayList<ContactLists> listData;
ArrayList<ContactLists> mStringFilterList;
ValueFilter valueFilter;
...
...
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ContactLists> filterList = new ArrayList<ContactLists>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
ContactLists mydata = new ContactLists(mStringFilterList.get(i)
.getName(), mStringFilterList.get(i)
.getNumber());
filterList.add(mydata);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
listData = (ArrayList<ContactLists>) results.values;
Log.e("XXXXXXXXX",results.values.toString());
notifyDataSetChanged();
}
}
Try this
public boolean onQueryTextChange(String newText) { myAdapter.getFilter().filter(newText);
Listview.setAdapter(myAdapter);return false; }

AutoCompleteTextView always display complete list instead of filtering

I want to filter the text in autocompletetextview. When i start to enter the text the instead of display filtered text it display the complete list of text. I mean it does not filter the list.
Please help me to solve the issue. I tried but could not recognize the problem
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> mData;
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mData = new ArrayList<String>();
mData.add("one");
mData.add("oneee");
mData.add("two");
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int index) {
return mData.get(index);
}
#Override
public Filter getFilter() {
Filter myFilter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
List<String> resultsSuggestions = new ArrayList<String>();
if(constraint != null) {
for (int i = 0; i < getCount(); i++) {
if(getItem(i).toString().startsWith(constraint.toString())){
resultsSuggestions.add(getItem(i).toString());
}
}
}
FilterResults results = new FilterResults();
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence contraint, FilterResults results) {
try {
ArrayList<String> newValues = (ArrayList<String>) results.values;
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
} catch(Exception e) {
Log.v("Near ATM", "Exception ::" + e.getMessage());
}
}
};
return myFilter;
}
}
I would say your problem is in publishResults method. You should call notifyDataSetChanged() even if result.count == 0. And your getItem and getCount methods should work with filtered list. So your code should look like this:
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> fullList;
private ArrayList<String> filtredList;
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
filtredList = new ArrayList<String>();
fullList = new ArrayList<String>();
fullList.add("one");
fullList.add("oneee");
fullList.add("two");
}
#Override
public int getCount() {
return filtredList.size();
}
#Override
public String getItem(int index) {
return filtredList.get(index);
}
#Override
public Filter getFilter() {
Filter myFilter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
List<String> resultsSuggestions = new ArrayList<String>();
if(constraint != null) {
for (int i = 0; i < fullList.size(); i++) {
if(fullList.get(i).startsWith(constraint.toString())){
resultsSuggestions.add(fullList.get(i));
}
}
}
FilterResults results = new FilterResults();
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
filtredList = resultsSuggestions;
return results;
}
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence contraint, FilterResults results) {
try {
ArrayList<String> newValues = (ArrayList<String>) results.values;
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if(results != null) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
} catch(Exception e) {
Log.v("Near ATM", "Exception ::" + e.getMessage());
}
}
};
return myFilter;
}
}

Categories

Resources