[Auction List Image][1] I am trying to implement a search function to search through the ListView by a specific TextView which is the ItemName TextView as shown in the image such as "Adidas Shoes" & "Nike Shoes" .That TextView is id as txtName. Currently, with these codes, there is no error, but the search function is not doing anything. How do I implement the search to actually search by looking through the txtName TextView?
Adapter:
public class AuctionListAdapter extends BaseAdapter implements Filterable {
ValueFilter valueFilter;
private Context context;
private int layout;
private ArrayList<Model> auctionList;
public AuctionListAdapter(Context context, int layout, ArrayList<Model> auctionList) {
this.context = context;
this.layout = layout;
this.auctionList = auctionList;
}
#Override
public int getCount() {
return auctionList.size();
}
#Override
public Object getItem(int position) {
return auctionList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
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<Model> filterList = new ArrayList<Model>();
for (int i = 0; i < auctionList.size(); i++) {
if ((auctionList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Model model = new Model(auctionList.get(i).getId(),auctionList.get(i).getName(),
auctionList.get(i).getDescription(),auctionList.get(i).getPrice(),auctionList.get(i).getDuration()
,auctionList.get(i).getImage());
filterList.add(model);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = auctionList.size();
results.values = auctionList;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
auctionList = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}
}
private class ViewHolder{
ImageView imageView;
TextView txtName,txtDescription,txtPrice,txtDuration;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout,null);
holder.txtName=row.findViewById(R.id.txtName);
holder.txtDescription=row.findViewById(R.id.txtDescription);
holder.txtPrice=row.findViewById(R.id.txtPrice);
holder.txtDuration=row.findViewById(R.id.txtDuration);
holder.imageView=row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}
Model model = auctionList.get(position);
holder.txtName.setText(model.getName());
holder.txtDescription.setText(model.getDescription());
holder.txtPrice.setText(model.getPrice());
holder.txtDuration.setText(model.getDuration());
byte[] auctionImage = model.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(auctionImage,0,auctionImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
AuctionList.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu,menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Create a private class inside your adapter
/**
* Custom filter for Brand list
* Filter content in brand list according to the search text
*/
private class BrandFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint!=null && constraint.length()>0) {
ArrayList<User> tempList = new ArrayList<User>();
// search content in Brand list
for (User user : brandList) {
if (mList.getBrandName().toLowerCase().contains(constraint.toString().toLowerCase())) {
tempList.add(user);
}
}
filterResults.count = tempList.size();
filterResults.values = tempList;
} else {
filterResults.count = friendList.size();
filterResults.values = friendList;
}
return filterResults;
}
/**
* Notify about filtered list to ui
* #param constraint text
* #param results filtered result
*/
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<User>) results.values;
notifyDataSetChanged();
}
}
Implement Filterable to your adapter , and write the following code to your Override function getfilter.
if (brandFilter == null) {
Filter = new BrandFilter();
}
return friendFilter;
Just call this method inside your activity , on onQueryTextChange
mAdapetr.getFilter().filter(newText);
For temp list :
private ArrayList<Model> filteredList;
and in your constructor
this.filteredList = auctionList
while publishing result , /**
* Notify about filtered list to ui
* #param constraint text
* #param results filtered result
*/
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<Model>) results.values;
notifyDataSetChanged();
}
Related
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();
}
};
}
}
This is the code I am using for custom adapter for my autocomplete textview
I tried this solution link
This solution says that item count may be 0 but in my case item count is not 0.
It never goes to getView() method. I try it calling from Fragment, I tried calling it from Activity
I tried solution from this answer as well link but it doesn't work.
It never goes inside the getView() method.
public class BusCityListAdapter extends ArrayAdapter<BusCityList> {
private ArrayList<BusCityList> cityList;
private ArrayList<BusCityList> tempCityList;
private ArrayList<BusCityList> suggestionsList;
public BusCityListAdapter(Context context, ArrayList<BusCityList> objects) {
super(context, android.R.layout.simple_list_item_1, objects);
this.cityList = objects;
this.tempCityList = new ArrayList<BusCityList>(objects);
this.suggestionsList = new ArrayList<BusCityList>(objects);
}
#Override
public int getCount() {
System.out.println("Main List Size=="+cityList.size());
System.out.println("Temp List Size=="+tempCityList.size());
System.out.println("Suggestion List Size=="+suggestionsList.size());
return cityList.size();
}
#Override
public BusCityList getItem(int position) {
return cityList.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("Inside GetView");
BusCityList busCity = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.bus_city_name_layout, parent, false);
}
MyTextView bus_name_label = (MyTextView) convertView.findViewById(R.id.bus_name_label);
if (bus_name_label != null)
bus_name_label.setText(busCity.getCity_name());
// Now assign alternate color for rows
return convertView;
}
#Override
public Filter getFilter() {
return myFilter;
}
Filter myFilter = new Filter() {
#Override
public CharSequence convertResultToString(Object resultValue) {
BusCityList busCity = (BusCityList) resultValue;
return busCity.getCity_name();
}
#Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestionsList.clear();
for (BusCityList city : tempCityList) {
if (city.getCity_name().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
suggestionsList.add(city);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestionsList;
filterResults.count = suggestionsList.size();
return filterResults;
} else {
return new FilterResults();
}
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<BusCityList> c = (ArrayList<BusCityList>) results.values;
if (results != null && results.count > 0) {
clear();
for (BusCityList city : c) {
add(city);
notifyDataSetChanged();
}
}
}
};
}
Here is the code where I am setting the adapter to AutoCompleteTextView
AutoCompleteTextView txtToWhere = (AutoCompleteTextView) v.findViewById(R.id.txtToWhere);
busCityListAdapter = new BusCityListAdapter(getActivity(), arrayList);
txtToWhere.setAdapter(busCityListAdapter);
busCityListAdapter.notifyDataSetChanged();
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();
}
I have custom listview and I have implemented SearchView to filter data. When we type something for search listview get filter correctly, but when I clear some characters from search text listview doesn't filter data. It remain constant at last search position.
Let me explain with screenshots.
Here, List is not yet filtered. all item are present.
I am searching in two fields of each item(name and description).
In this screen I have searched from cam it shows me correct result.
I have clear text till c and I am expecting resut should be two item i.e. camera and LED TV.
Here is my code.
RateListAdapter.java
public class RateListAdapter extends BaseAdapter implements Filterable {
Context context;
private SparseBooleanArray mSelectedItemsIds;
public ArrayList<RateList> itemRateList;
public LayoutInflater inflater = null;
public ArrayList<RateList> tempList;
CustomFilter filter;
public RateListAdapter(Context context, ArrayList<RateList> itemRateList) {
this.context = context;
this.itemRateList = itemRateList;
mSelectedItemsIds = new SparseBooleanArray();
this.tempList = itemRateList;
}
#Override
public int getCount() {
return itemRateList.size();
}
#Override
public RateList getItem(int i) {
return itemRateList.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
class Holder {
TextView tv_name, tv_description, tv_amount;
ImageView iv_itemImage;
}
#Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
Holder h = new Holder();
RateList rateList = itemRateList.get(i);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.rate_list_item, viewGroup, false);
}
h.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
h.tv_description = (TextView) convertView.findViewById(R.id.tv_description);
h.tv_amount = (TextView) convertView.findViewById(R.id.tv_amount);
h.iv_itemImage = (ImageView) convertView.findViewById(R.id.iv_itemImage);
h.tv_name.setText(rateList.Name);
h.tv_description.setText(rateList.description);
h.tv_amount.setText("₹ "+rateList.amount);
Picasso.with(context).load(rateList.image).resize(92,92).into(h.iv_itemImage);
return convertView;
}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
#Override
public Filter getFilter() {
if(filter == null)
{
filter=new CustomFilter();
}
return filter;
}
private class CustomFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
if(constraint != null && constraint.length()>0) {
constraint=constraint.toString().toLowerCase();
ArrayList<RateList> filters=new ArrayList<RateList>();
for(int i=0;i<itemRateList.size();i++) {
if (itemRateList.get(i).Name.toLowerCase().contains(constraint) ||
itemRateList.get(i).description.toLowerCase().contains(constraint)) {
RateList r = new RateList();
r.Name = itemRateList.get(i).Name;
r.description= itemRateList.get(i).description;
r.id = itemRateList.get(i).id;
r.image = itemRateList.get(i).image;
r.amount = itemRateList.get(i).amount;
filters.add(r);
}
}
results.count = filters.size();
results.values = filters;
} else {
results.count=itemRateList.size();
results.values=itemRateList;
}
return results;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
itemRateList=(ArrayList<RateList>) results.values;
notifyDataSetChanged();
}
}
}
searchview code of
BookingFrament.java
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String searchQuery) {
rateListAdapter.getFilter().filter(searchQuery);
return false;
}
});
Please help.
This is because you are modifying the itemRateList
Check this method:
#Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
itemRateList=(ArrayList<RateList>) results.values;
notifyDataSetChanged();
}
I will suggest to store the result in a separate arrayList and use it in for you adapter class.
Due to the above result being updated in the original itemRateList, so next time when the performFiltering method is invoked. You are iterating just the previous result and NOT the original list.
Hope this helps.
Good day, I have this custom adapter with a filterable interface implemented and am getting duplicate values in the resulting list.
SearchAutoCompleteAdapter.java
public class SearchAutoCompleteAdapter extends BaseAdapter implements Filterable {
private ArrayList<BaseAutocompleteItems> resultList;
List<BaseAutocompleteItems> filteredProducts;
private LayoutInflater layoutInflater;
private Context context;
private int layout;
SearchAutoCompleteAPI searchautocomplete = new SearchAutoCompleteAPI();
public SearchAutoCompleteAdapter(Context context, int resource) {
super();
this.context = context;
this.layout = resource;
filteredProducts = new ArrayList<BaseAutocompleteItems>();
resultList = new ArrayList<BaseAutocompleteItems>();
}
#Override
public int getCount() {
return resultList.size();
}
#Override
public Object getItem(int index) {
return resultList.get(index);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(layout, null);
}
TextView name = (TextView) convertView.findViewById(R.id.suggestion_text_id);
name.setText(resultList.get(position).getName());
return convertView;
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
List<BaseAutocompleteItems> tempfilteredProducts = new ArrayList<BaseAutocompleteItems
filteredProducts.clear();
if (constraint != null || constraint.length() > 0) {
tempfilteredProducts.clear();
tempfilteredProducts = searchautocomplete.autocomplete(constraint.toString()); //webservice call
} else {
tempfilteredProducts = new ArrayList<BaseAutocompleteItems>();
}
for (BaseAutocompleteItems items : tempfilteredProducts) {
if (items.getName().contains(constraint.toString())) {
filteredProducts.add(items);
}
}
filterResults.values = filteredProducts;
filterResults.count = filteredProducts.size();
return filterResults;
}
#Override
protected void publishResults (CharSequence constraint, FilterResults results){
resultList = (ArrayList<BaseAutocompleteItems>)results.values;
if(results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
;
return filter;
}
}
If I type "yell" and press backspace for "yel" or increase my char to "yello", I get the same result and thus the ArrayList ends up with duplicated items. I have tried clearing the lists before populating the list but nothing seems to work.
Nothing wrong with the code in the question. just a checklist for anyone first. make sure you call Clear() on the ArrayList being returned in the line.
tempfilteredProducts = searchautocomplete.autocomplete(constraint.toString()); //webservice call
from the API call first before populating the values from the webservice and sending it back to tempfilteredProducts(i.e before every api request). That way you avoid duplicate values from the autocompletetextview string as in my case in the question.
Try changing
if (items.getName().contains(constraint.toString()))
to
if (items.getName().startsWith(constraint.toString()))