1

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);
                }


            };
hkaraoglu
  • 1,345
  • 1
  • 15
  • 34

1 Answers1

0

You can achieve the same by just modifying your filter logic.

While comparing your, instead of using startWith(), use contains() in your filter logic, in adapter, it will return list whose name contains "ger".

Something like this...

String aa= "";
String bb = "";
aa.contains(bb);

Create Filter Object with all required implementations, and with your logic.

Filter customFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
        }
      @Override
      protected void publishResults(CharSequence contraint, FilterResults results) {
      }
}

Then override, your adapter's filter method and return customFilter object.

 @Override
 public Filter getFilter() {
    return customFilter;
}

Refer to this solution to know more...

Custom getFilter in custom ArrayAdapter in android

Community
  • 1
  • 1
Ritt
  • 3,181
  • 3
  • 22
  • 51
  • 1
    ohh.. sorry my mistake, it would be contains() instead of containsWith(). – Ritt Mar 25 '16 at 15:08
  • How and Where should I use? I'm using ArrayAdapter. – hkaraoglu Mar 25 '16 at 15:14
  • okay, post your getFilter() method logic. You have to use it inside that method, where you are filtering your result. Post the method. – Ritt Mar 25 '16 at 15:16
  • It's android's self method – hkaraoglu Mar 25 '16 at 15:25
  • yeah, you need to override the filter logic in adapter class. – Ritt Mar 25 '16 at 15:29
  • I'm using ArrayAdapter. Arrayadapter uses self private ArrayFilter class. I can't override this. Becouse it uses arrayadapter's private variables. And these variables are not protected. So I cannot use. Is there any way? – hkaraoglu Mar 25 '16 at 15:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107350/discussion-between-revenge-and-ritesh). – hkaraoglu Mar 25 '16 at 16:05