I have a ListView
. I want to search in Listview
by arrayAdapter
. Android textwatcher
helped me in this topic. But it searches only one to one coupling. For example: I type "ger" to search Germany. It lists just "Germany", but my list also contains Algeria and Nigeria. So, how can I search with like %% command as SQL in adapter?
public ArrayAdapter<?> adapter;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
LayoutInflater inflater = LayoutInflater.from(getActivity());
View dialogLayout = null;
if (adapter != null) {
dialogLayout = inflater.inflate(R.layout.layout_dialog_list_filter, null);
filterText = (EditText) dialogLayout.findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
ListView listView = (ListView) dialogLayout.findViewById(R.id.dialogListView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(listItemClickListener);
listView.setTextFilterEnabled(true);
}
builder.setNegativeButton(R.string.text_cancel, this);
builder.setView(dialogLayout);
return builder.create();
}
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
adapter.getFilter().filter(s);
}
};