How to Filter ListView through EditText

22,540

Solution 1

try this one,whenever u enter text in the edittext ,the list will show filtered result as i have shown the stuff in images

Initial:

Initial

Filtered:

Filtered:
this is main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText 
    android:id="@+id/etSearchbox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lvFirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    ></ListView>    

</LinearLayout>

this is FilterListActivity.java

package com.filterlist;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListActivity extends Activity{

EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);

adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);   
lvFirst.setAdapter(adapter1);

etSearchbox.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        FilterListActivity.this.adapter1.getFilter().filter(arg0);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }
});




}
}

Solution 2

I have a working example, try this:

filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);

TextWatcher filterTextWatcher = new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {  

        }  
        public void onTextChanged(CharSequence s, int start, int before,int count) {  
            adapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub              
        }  
    };

The adapter must implements Filterable

            @Override
            public Filter getFilter() {
//              Filter filter = null;

                if(filter == null)
                    filter = new CheeseFilter();
                return filter;
            }

And the filter class:

        public class CheeseFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub

                constraint = constraint.toString().toLowerCase();

                FilterResults newFilterResults = new FilterResults();

                if (constraint != null && constraint.length() > 0) {


                    ArrayList<String> auxData = new ArrayList<String>();

                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).toLowerCase().contains(constraint))
                            auxData.add(data.get(i));
                    }

                    newFilterResults.count = auxData.size();
                    newFilterResults.values = auxData;
                } else {

                    newFilterResults.count = data.size();
                    newFilterResults.values = data;
                }

                return newFilterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                ArrayList<String> resultData = new ArrayList<String>();

                resultData = (ArrayList<String>) results.values;

                EfficientAdapter adapter = new EfficientAdapter(context, resultData);
                list.setAdapter(adapter);

//              notifyDataSetChanged();
            }

        }

You can check this post for more info:

Filter expandableList

Filter ListView

Solution 3

when you use Custom listView

Adapter :

public class Adapter extends ArrayAdapter {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> filteredData = new ArrayList<>();

public Adapter(@NonNull Context context, int resource) {
    super(context, resource);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    @SuppressLint("ViewHolder") View vi = inflate.inflate(R.layout.ly_items, null);
    try {
        JSONObject js = new JSONObject(list.get(position));
        TextView txtItem = vi.findViewById(R.id.txtItem);
        ImageView imgItem = vi.findViewById(R.id.imgItem);
        txtItem.setText(js.getString("name") + " - " + js.getInt("number"));
        Picasso.get().load(js.getString("logo_url")).into(imgItem);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return vi;
}

@Override
public void add(@Nullable Object object) {
    super.add(object);
    list.add(object.toString());
    filteredData.add(object.toString());
}

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

@Nullable
@Override
public Object getItem(int position) {
    return list.get(position);
}


public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    list.clear();
    if (charText.length() == 0) {
        list.addAll(filteredData);
    } else {
        for (String wp : filteredData) {

            try {
                JSONObject json = new JSONObject(wp);
                if (json.getString("name").toLowerCase().contains(charText) || json.getString("number").contains(charText)) {
                    list.add(wp);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    notifyDataSetChanged();
    }
}

And your class:

    Adapter adapter;
ListView list;
EditText edtSearch;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

list = findViewById(R.id.list);

edtSearch = findViewById(R.id.edtSearch);

 adapter = new Adapter(this, android.R.layout.simple_list_item_1);


list.setAdapter(adapter);

edtSearch.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) {
                adapter.filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }
Share:
22,540
Mordiggian
Author by

Mordiggian

Updated on July 09, 2022

Comments

  • Mordiggian
    Mordiggian almost 2 years

    I'm making an eBook like application for Android, and I want to filter the title of the book but every time that you put a word or sentence in the edittext it will search the content of the books.

    Can someone help me with this...