Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support searching with 'contains' instead 'startWith' #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.toptoche.searchablespinnerlibrary;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import java.util.ArrayList;
import java.util.List;

public class SearchableAdapter extends ArrayAdapter<String> implements Filterable {

private ArrayList<String> items;
private ArrayList<String> filteredItems;

SearchableAdapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
this.items = objects;
this.filteredItems = objects;
}

@Override
public int getCount() {
return filteredItems.size();
}

@Override
public String getItem(int position) {
return filteredItems.get(position);
}

private List<String> getAllItems() {
return items;
}

@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<String> filteredItems = new ArrayList<>();
List<String> items = getAllItems();

if (constraint == null || constraint.length() == 0) {
results.values = items;
results.count = items.size();
} else {
for (String item : items) {
if (item.toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredItems.add(item);
}
}
results.values = filteredItems;
results.count = filteredItems.size();
}

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredItems = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SearchableListDialog extends DialogFragment implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener {

private static final String ITEMS = "items";

private ArrayAdapter listAdapter;
private SearchableAdapter listAdapter;

private ListView _listViewItems;

Expand Down Expand Up @@ -152,12 +152,12 @@ private void setData(View rootView) {
mgr.hideSoftInputFromWindow(_searchView.getWindowToken(), 0);


List items = (List) getArguments().getSerializable(ITEMS);
ArrayList items = (ArrayList) getArguments().getSerializable(ITEMS);

_listViewItems = (ListView) rootView.findViewById(R.id.listItems);

//create the adapter by passing your ArrayList data
listAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,
listAdapter = new SearchableAdapter(getActivity(), android.R.layout.simple_list_item_1,
items);
//attach the adapter to the list
_listViewItems.setAdapter(listAdapter);
Expand Down Expand Up @@ -196,9 +196,9 @@ public boolean onQueryTextChange(String s) {
// listAdapter.filterData(s);
if (TextUtils.isEmpty(s)) {
// _listViewItems.clearTextFilter();
((ArrayAdapter) _listViewItems.getAdapter()).getFilter().filter(null);
listAdapter.getFilter().filter(null);
} else {
((ArrayAdapter) _listViewItems.getAdapter()).getFilter().filter(s);
listAdapter.getFilter().filter(s);
}
if (null != _onSearchTextChanged) {
_onSearchTextChanged.onSearchTextChanged(s);
Expand Down