How can I filter ListView data when typing on EditText in android
- Add
TextWatcher
toEditText#addTextChangedListener
- In
onTextChanged
add or remove items from yourListView
's adapter. If you are subclassingArrayAdapter
it would haveadd
andremove
methods
How to filter ListView using a EditText when enter is pressed?
Instead of this:
final EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
...
public void onTextChanged(CharSequence s, int start,int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
Try below:
final EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.setImeActionLabel("Filter",1);
myFilter.setPrivateImeOptions("actionUnspecified");
myFilter.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == 1 || id == EditorInfo.IME_NULL) {
dataAdapter.getFilter().filter(textView.getText().toString());
return true;
}
return false;
}
});
Filter list view from edit text
My problem is solved, found out that I have to override getCount() and getItem().
EditText to filter items in listview. How to?
Try like this
I changed the adapter class
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appsList = null;
private Context context;
private PackageManager packageManager;
private List<ApplicationInfo> listOfApp;
public ApplicationAdapter(Context context, int textViewResourceId,
List<ApplicationInfo> appsList) {
super(context, textViewResourceId, appsList);
this.context = context;
this.appsList = appsList;
this.listOfApp = new ArrayList<ApplicationInfo>(); //Added here
packageManager = context.getPackageManager();
listOfApp.addAll(appsList);
}
@Override
public int getCount() {
return ((null != appsList) ? appsList.size() : 0);
}
@Override
public ApplicationInfo getItem(int position) {
return ((null != appsList) ? appsList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.snippet_list_row, null);
}
ApplicationInfo data = appsList.get(position);
if (null != data) {
TextView appName = (TextView) view.findViewById(R.id.app_name);
TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);
appName.setText(data.loadLabel(packageManager));
packageName.setText(data.packageName);
iconview.setImageDrawable(data.loadIcon(packageManager));
}
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
appsList.clear();
if (charText.length() == 0) {
appsList.addAll(listOfApp);
}
else
{
for (ApplicationInfo ai : listOfApp)
{
if (ai.loadLabel(packageManager).toString().toLowerCase(Locale.getDefault()).contains(charText))
{
appsList.add(ai);
}
}
}
notifyDataSetChanged();
}
}
and change in Activity
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// I changed here to call filter method from Adapter class.
String text =inputSearch.getText().toString().toLowerCase(Locale.getDefault());
listadaptor.filter(text);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
Hope this help you.
Filter ListView with EditText
Your list based on bean rather than beanFiltered. Thats why notifyDataSetChanged not working. getCount() and getItem(int position) always working on bean. Thats why filter not working. Please Change according below:
@Override
public int getCount() {
return beanFiltered.size();
}
@Override
public Object getItem(int position) {
return beanFiltered.get(position);
}
how to display listview while typing on edittext without using filter?
//imports
public class SearchActivity extends Activity implements TextWatcher{
private EditText searchField;
private SearchActivity activity;
private ListView listView;
protect void onCreate(Bundle savedInstances){
setContentView(R.layout.yourlayout);
activity = this;
searchField = (EditText)findViewById(R.id.searchField);
searchField.addTextChangedListener(this);
listView = (ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(Adapter<?> arg0, View arg1, int position, long arg3){
//action when an item is clicked
}
});
}
@Override
public void afterTextChanged(final Editable searchField){
listView.setAdapter(null); //refresh list items
if(searchField.length()>0){
//do your searches here
listView.setAdapter(new YourCustomAdapter());
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
}
Related Topics
Android Webview Click Open Within Webview Not a Default Browser
What Is the Use of Movetofirst () in SQLite Cursors
How to Pause Frame Animation Using Animationdrawable
Stuck with Getting Camera Pic When Using the Tab Activity
Reusing Fragments in a Fragmentpageradapter
How to Get a Jacoco Coverage Report Using Android Gradle Plugin 0.10.0 or Higher
Webservice Credentials - Openid/Android Accountmanager
Image Size for All Screen Devices
Validation Allow Only Number and Characters in Edit Text in Android
Pagination Not Work for the Recyclerview Within Nestedscrollview
How to Use the Gradle Build System for Android with Eclipse
Android Devices with Different Height Takes Same Layout Folder
Manipulating Data on Webs in Android
Tab Not Taking Full Width on Tablet Device [Using Android.Support.Design.Widget.Tablayout]
Android 4.3: How to Connect to Multiple Bluetooth Low Energy Devices