Question

I have a AutoCompleteTextView that shows suggested videos. When user enter text in it, my application gets the data from server based on the text user type. But when user changes the text(enter a character by character), the Dropdown is automatically dismissed. Then I get the data and show the Dropdown again. So my Dropdown is dismissed and shown immediately. It causes a bad effect to user (especially when user types fast)
How can I avoid the Dropdown is dismissed when user changes the text. So that when I get the data from server, I can call myAdapter.notifyDataChanged().

Thanks in advance. P/S: I heard a solution is extend the AutoCompleteTextView. But I don't know to do that.

Was it helpful?

Solution 2

Here is my solution.

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

    public CustomAutoCompleteTextView(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub     
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.autoCompleteTextViewStyle);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean enoughToFilter() {
        // TODO Auto-generated method stub
        return true;
    }
}
public class SearchSuggestionAdapter extends ArrayAdapter<SearchObject>
{   
.......
@Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        Filter filter = new Filter() {
            String keyword;

            @Override
            public CharSequence convertResultToString(Object resultValue) {
                // TODO Auto-generated method stub
                return keyword;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                // TODO Auto-generated method stub
                if(results.values != null)
                    notifyDataSetChanged();
                else
                    notifyDataSetInvalidated();
            }       

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub
                keyword = constraint.toString();
                final FilterResults filterResults = new FilterResults();
                filterResults.values = mList;
                filterResults.count = mList.size();  
                return filterResults;
};
        return filter;
    }   
}

Because I got data from the server. So in performFiltering, I just return a non-null object.
Hope it helpful to you.

OTHER TIPS

Implements Filterable in adapter, and use Filter.

try this :

public class ListAdapter extends ArrayAdapter<String> implements Filterable{


    private List<String> listResult;

    ...
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Assign the data to the FilterResults
                    filterResults.values = listResult;
                    filterResults.count = listResult.size();
                    }
                return filterResults;
                }

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

I guessed that, you are getting a list of data from the server based on the string in the autocomplete textview. So that the adapter loads the filtered list whenever the text changes. Try to implement CursorAdapter. Its the better solution for your problem. Just try and let me know.

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