Question

I have AutoCompleteTextView, when i select anitem in the drop down list, the value insert into form, but when i focus to the other view, for example edittext - my text set invisible. When i return my focus to the AutoCompleteTextView again - my text set visible again. What is the problem ?

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:orientation="vertical"
        android:paddingRight="20dp"
        android:paddingLeft="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/account_code"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ems="20"
            android:inputType="number"
            android:hint="Account Code" />

        <AutoCompleteTextView
            android:id="@+id/search_book_data"
            android:completionThreshold="1"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:imeOptions="actionSearch"
            android:hint="Book data">
        </AutoCompleteTextView>

        </LinearLayout>
    </LinearLayout>
</ScrollView>

I used custom adapter:

private class BookSearchAdapter extends BaseAdapter implements Filterable {
    private LayoutInflater inflater;
    private List<Book> originBookData;
    private List<Book> fullBookData;
    private BookFilter filter;

    public BookSearchAdapter(Context context) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        filter = new BookFilter();
    }

    public void addAll(List<Book> bookData) {
        this.originBookData = bookData;
        fullBookData = bookData;
    }

    @Override
    public int getCount() {
        return originBookData == null ? 0 : originBookData.size();
    }

    @Override
    public Book getItem(int i) {
        return originBookData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.search_item, viewGroup, false);
        }
        Book book = getItem(i);
        TextView bookTitle = (TextView) view.findViewById(R.id.book_name);
        bookName.setText(book.getName());
        TextView bookPrice = (TextView) view.findViewById(R.id.book_price);
        bookprice.setText("" + book.getPrice());
        return view;
    }

    @Override
    public Filter getFilter() {
        return filter;
    }

    private class BookFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults oReturn = new FilterResults();
            String keyCharacters = (String) charSequence;
            ArrayList<Book> results = new ArrayList<Book>();

            if (fullBookData == null) {
                fullBookData = new ArrayList<Book>(originBookData);
            }

            if (keyCharacters == null || keyCharacters.length() == 0) {
                List<Book> list = new ArrayList<Book>(fullBookData);
                oReturn.values = list;
                oReturn.count = list.size();
            } else {
                keyCharacters = keyCharacters.toLowerCase();
                if (books != null && books.size() > 0) {
                    for (Book book : originBookData) {
                        if (book.getName().toLowerCase().contains(keyCharacters)
                                || ("" + book.getPrice()).toLowerCase().contains(keyCharacters.)
                            results.add(book);
                    }
                }
                oReturn.values = results;
                oReturn.count = results.size();
            }
            return oReturn;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            originBookData = filterResults.values != null ? (ArrayList<Book>) filterResults.values : fullBookData;
            if (filterResults.count > 0)
                notifyDataSetChanged();
            else
                notifyDataSetInvalidated();
        }
    }
}
Was it helpful?

Solution

This seems to be a bug that I faced a time ago, basically I solved it setting two additional attributes on AutoCompleteTextView:

  • autoCompleteTextViewStyle
  • textColor

I know it's a mess, but try it and if it works, try finding a combination of those attributes that fit to your layout.

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