Can someone help me to filter the list using a custom adapter I have been stuck on this for sometime now.. I have followed logcat and It does output the filtered results my problem is updating the listview with the filtered results.
// list of data items
private static List<ListData> mDataList = Arrays.asList(
new ListData("Apple"),
new ListData("Banana"),
new ListData("Caramel"),
new ListData("Dog"),
new ListData("Cat"),
new ListData("Snake"),
new ListData("Lion"),
new ListData("Zebra"),
new ListData("Ant"),
new ListData("Bee"),
new ListData("Bird"),
new ListData("Leopard"),
new ListData("Flamingo"),
new ListData("Tusk"),
new ListData("Elephant"),
new ListData("Chicken")
);
SampleAdapter work = new SampleAdapter();
private Button btnoe;
private AutoCompleteTextView aoe;
private TextView txtHistory, ao;
private ListView lv;
private String[] places;
private String p = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
final EditText e = (EditText) findViewById(R.id.edt_search);
// init the list view and its adapter
final ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(work);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
e.setText("" + mDataList.get(position).data);
}
});
//setFields();
e.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
work.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private static class ListData {
private String data;
public ListData(String data) {
this.data = data;
}
}
private class SampleAdapter extends BaseAdapter implements Filterable {
private List<String> filtered = null;
private ItemFilter mFilter = new ItemFilter();
@Override
public int getCount() {
return mDataList.size();
}
@Override
public ListData getItem(int position) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<ListData> list = mDataList;
int count = list.size();
final List<String> nlist = new ArrayList<>(count);
String FilterableString;
for (int i = 0; i < count; i++) {
FilterableString = list.get(i).data;
if (FilterableString.toLowerCase().contains(filterString)) {
mDataList.contains(FilterableString);
Log.d("mDataList", ""+mDataList.get(i).data);
nlist.add(FilterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
int pos = 0;
String s = "";
if (convertView == null) {
convertView = View.inflate(ListActivity.this, R.layout.sample_list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListData item = getItem(position);
s = item.data.substring(0, 1);
holder.textView.setText(item.data);
for (pos = 0; pos > mDataList.size(); pos++) {
s = mDataList.get(pos).data.substring(0, 1);
}
convertView.findViewById(R.id.imageView);
return convertView;
}
}
private class ViewHolder {
private View view;
private ImageView imageView, checkIcon;
private TextView textView;
private ViewHolder(View view) {
this.view = view;
imageView = (ImageView) view.findViewById(R.id.imageView);
textView = (TextView) view.findViewById(R.id.textView);
checkIcon = (ImageView) view.findViewById(R.id.check_icon);
}
}
}