Connect TextWatcher with a custom adapter for filtering text through EditText

I am new to Android development, and I wrote some codes using an example from a site about a custom ListView , now with ListView I want to add an EditText that filters the text and displays only what I wrote in the ListView .

Currently, when I print something, the ListView value becomes empty and nothing is displayed, but when I delete my input, the ListView restored with the original elements.

This is my adapter class:

 public class EntryAdapter extends ArrayAdapter<Item> { private Context context; private ArrayList<Item> items; private LayoutInflater vi; public Context conx; public EntryAdapter(Context context, ArrayList<Item> items) { super(context,0, items); this.context = context; this.items = items; vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } 

And this is my data insert for main-class

  items.add(new SectionItem("letters")); items.add(new EntryItem("a","subtitle","image")); items.add(new EntryItem("b","subtitle","image")); 

And this is my textWatcher , the simple one I created:

 inputSearch = (EditText) findViewById(R.id.inputSearch); inputSearch.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // When user changed the Text StartClass.this.adapter.getFilter().filter(cs); } @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 } }); 

And here is the use of adapter and setting ListView

 EntryAdapter adapter = new EntryAdapter(this, items); x = adapter; listview.setAdapter(adapter); 

Can someone tell me what I'm doing wrong? I tried to see a couple of tutorials, but no one where it would be good to show how to implement it, I implemented it, but it does not work. Any ideas that I can skip.

+5
source share

Source: https://habr.com/ru/post/1238286/


All Articles