1

I have a String[] called lv_arr, and my activity looks like this:

which looks like this

It contains a EditText and below that I am displaying a above lv_arr[] using this code:

listView = (ListView) findViewById(R.id.listview);  
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,lv_arr);  
listView.setAdapter(adapter);

Currently lv_arr[] contains only one string (i.e "Notes"), and whenever the user adds a string it will be updated. I want to add TextWatcher on my EditText, so I tried the following:

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/addLabelText"
        android:hint="Add Label">
        <requestFocus/>

    </EditText>

With:

addLabelText = (EditText) findViewById(R.id.addLabelText);
        addLabelText.addTextChangedListener(listWatcher);
}

private  final TextWatcher listWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
            System.out.println(s);
        }

        @Override
        public void onTextChanged(CharSequence s, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

I want a final output which, whenever user types anything in that EditText, the significant values from my list should be displayed below it. When that EditText is goes to a blank state, the below list should display all elements. Otherwise only matching values should be visible.

I tried using a for loop so that I can get matching values, but I am making a mistake somewhere...

Bryan
  • 14,756
  • 10
  • 70
  • 125
varsha valanju
  • 801
  • 1
  • 9
  • 27

5 Answers5

0

Use Filterable.

And here it is post for example.

Community
  • 1
  • 1
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
0

I think you can achieve this using filterable.

Try like below:

private  final TextWatcher listWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
       adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

Implement Filterable to Adapter and code like below

 @Override
public Filter getFilter() {
    if (itemFilter == null) {
        itemFilter = new ItemFilter();
    }
    return itemFilter;

}

private class ItemFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        final String searchString = constraint.toString().toLowerCase();
        Log.i("TAG", "Query=>" + constraint);
        final FilterResults filterResults = new FilterResults();

        if (constraint != null && constraint.toString() != "") {
            doctorsList.clear();
            // search content in DoctorList list
            for (i=0;i<itemNameArray.length();i++) {
                if (itemNameArray[i].toLowerCase().contains(searchString)) {
                    arrayList.add(itemNameArray[i]);

                }
            }

            filterResults.count = arrayList.size();
            Log.i("TAG", "FilterResultCount=>" + filterResults.count);
            filterResults.values = arrayList;
            Log.i("TAG", "FilterResultValues=>" + filterResults.values);

        } else {

            filterResults.count = arrayList.size();
            filterResults.values = arrayList;

        }
        return filterResults;

    }

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

        arrayList = (ArrayList<String>) results.values;
        notifyDataSetChanged();
        Log.i("TAG", "SearchResult" + arrayList);


    }
}
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
0

Enabling Search Filter

addLabelText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.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                          
    }
});
nzala
  • 374
  • 4
  • 10
0

So, to achieve my desired output i write code as follows:

1) i changed my EditText to SearchView

<SearchView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/addLabelText"
            />

2)added setOnQueryTextListener on SearchView

addLabelText =(SearchView) findViewById(R.id.addLabelText);
        addLabelText.setOnQueryTextListener(this);

3)implement QueryTextChangeListener 4)Then,

@Override
    public boolean onQueryTextSubmit(String query) {

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

Here adapter is the adapter that i used for my List View, i.e

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,lv_arr);
        listView.setAdapter(adapter);

Reference Link: http://www.tutorialsbuzz.com/2014/08/android-filters-for-simple-listview.html

varsha valanju
  • 801
  • 1
  • 9
  • 27
  • @vasrshavalanju Please Help me i have a dynamic list view i want to add text watcher . when i am write name it is filter on list view and show select list item that can i provide in edit text – Ashish Shahi Apr 26 '17 at 11:07
0

I am posting my answer here - might be useful for someone

I was trying to fix this - not able to get the position when list view -> edit text has a text watcher.

This is the solution that worked for me :

In get view -

when I am add text watcher listener to edit text, I also added the below line

edittext.setTag(R.id.position, position)

in your afterTextChanged - int position = edittext.getTag(R.id.position)

Gives the correct position number and you can do modifications based on the position number.