Question

I am loading the phone contacts ina list and implementing TextChangedListener on edittext as below

editTxt.addTextChangedListener(new TextWatcher() {

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

            public void afterTextChanged(Editable s) {
                final TextView noDataFound = (TextView) findViewById(R.id.norecords);
                inputName = s.toString();
                if(inputName!=null&&!inputName.trim().equals("")){

                Log.d(TAG, "LoadMoreEntries --> Constants.loadEntries : "
                        + Constants.loadEntries);

                    if (Constants.loadEntries != null) {
                        Constants.loadEntries.cancel(true);
                    }

                Constants.loadEntries = new LoadEntries();
                Constants.loadEntries.execute();
            }
                Button closesearch = (Button) findViewById(R.id.closesearch);

                if (inputName != null && !inputName.trim().equals("")) {
                    closesearch.setVisibility(View.VISIBLE);
                } else {
                    closesearch.setVisibility(View.GONE);
                }

                closesearch.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (Constants.loadEntries != null) {
                            Constants.loadEntries.cancel(true);
                Constants.loadEntries = new LoadEntries();
                Constants.loadEntries.execute();
                        }else {


            }
                        return false;
                    }
                }); 

            }
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }
        });

here when the user types the correct name it is giving the names and when he types wrong name it shows no data. my issue is when i typing the correct name and erasing, the whole list is loaded but when i type wrong name and displaying no data and when erasing the name, list is not updating. also i have " x " button after typing name and clicking on that should get all my list back. Any help is appreciated

Was it helpful?

Solution

Use Google Places AutoComplete API rather than implementing Textwatcher. Google Places AutoComplete API is really effective when you start type and take pause then it will show dropdown and dropdown list is updated at every character.

Using this you can easily update your dropdown list of your autocomplete.

Here is explanation of this.

editTxt.setAdapter(new PlacesAutoCompleteAdapter(this,R.layout.yourlayout));  

here is PlacesAutoCompleteAdapter class which is filter result and return filtered result.

private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
    private ArrayList<String> resultList;
    private String[] myArray;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return myArray.length;
    }

    @Override
    public String getItem(int index) {
        return myArray[index];
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    myArray = autocomplete(constraint.toString());  // here we are calling myAutocomplete method.                    
                    // Assign the data to the FilterResults
                    filterResults.values = myArray;
                    filterResults.count = myArray.length;
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }};
        return filter;
    }
}   

private String[] autocomplete(String dropdownString) {
    ArrayList<String> resultList = null;
    StringBuilder jsonResults = new StringBuilder();
    String term;

    try {
        term=URLEncoder.encode(dropdownString, "utf8");
    } catch (Exception e) {
        e.printStackTrace();
        term = dropdownString;
    }

    StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE);  
    sb.append("?param="+param1+"); // this is parameter if your getting data from server.        
    sb.append("&term="+term); // this term which you typing in edittext.  
    String url = sb.toString();

        // you can do here anything with your list. get it and populate it.   

    return myArray;
}

PLACES_API_BASE:- here is url if you are getting data from Web(in my example www.myurl/myapp).
TYPE_AUTOCOMPLETE:- file name or exact location from where are you getting data(in my example abc.php).
If you have any query ask me. don't be hesitate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top