【发布时间】:2023-03-05 08:19:01
【问题描述】:
我有一个AutoCompleteTextView 和ProgressBar 在它的右边,ProgressBar 的可见性在开始时是不可见的。
我正在使用自定义类 PlacesAutoCompleteAdapter 和包含 TextView 的 xml 文件自动完成在我的活动的 onCreate 中将适配器设置为 AutocompleteTextView,
actvFrom.setAdapter(new PlacesAutoCompleteAdapter(this,
R.layout.autocomplete));
在PlacesAutocompleteAdapter 类中,我正在实现Filterable 接口。
private class PlacesAutoCompleteAdapter extends ArrayAdapter<String>
implements Filterable {
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public android.widget.Filter getFilter() {
android.widget.Filter filter = new android.widget.Filter() {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(
final CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
// TODO Auto-generated method stub
resultList = autocomplete_fill_data(constraint
.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
};
return filter;
}
}
在 autocomplete_fill_data 函数中,我正在调用服务器并将其添加到 ArrayList myCollection 并返回它。
public ArrayList<String> autocomplete_fill_data(String value) {
myCollection = new ArrayList<String>();
// call to server
...
myCollection.add(result);
return myCollection;
}
AutoComplete 工作正常,但问题是我无法在调用服务器时显示ProgressBar。
我都试过了,
-
使用 addTextChangedListener
autocomplete.addTextChangedListener(new TextWatcher() {
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub if (s.length() >= 1) { System.out.println("Count ="+count); progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.INVISIBLE); } }
但问题在于,即使您停止输入文本,progressBar 仍会旋转。我想在用户停止编写文本后立即隐藏 ProgressBar。
- 在 performFiltering 中,我尝试隐藏 progressBar,但在您键入时自动完成框卡住了。
有没有其他方法可以在用户停止输入文本时尝试隐藏进度条。
【问题讨论】:
标签: android progress-bar autocompletetextview android-filterable