我有AutoCompleteTextView,当我在下拉列表中选择anitem时,值插入到表单中,但是当我专注于其他视图时,例如edittext - 我的文本设置为不可见。当我再次将焦点返回到AutoCompleteTextView时 - 我的文本集再次可见。有什么问题?
<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>
我使用自定义适配器:
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();
}
}
}
答案 0 :(得分:1)
这似乎是我前面遇到的一个错误,基本上我解决了它在AutoCompleteTextView
上设置两个额外属性:
我知道这很糟糕,但是试一试,如果有效,请尝试找到适合您布局的属性组合。