我有一个AutoCompleteTextView
和ProgressBar
在它的右边,ProgressBar
的可见性在开始时是不可见的。
我正在设置适配器AutocompleteTextView在onCreate我的活动与自定义类PlacesAutoCompleteAdapter和xml文件autocomplete其中包含TextView,
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
自动完成。addTextChangedListener(new texttwatcher () {
@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); } }
但问题是,即使你停止输入文本的进度条仍将旋转。我想在用户停止写入文本时立即隐藏进度条。
- 在performFiltering中,我试图隐藏进度条,但自动完成框在输入时卡住了。
是否有其他方法可以让我在用户停止输入文本时隐藏进度条
您应该尝试使用public void afterTextChanged(可编辑)
autocomplete.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
progressBar.setVisibility(View.GONE);
}