我有一个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;
}
自动完成工作正常,但问题是每当有服务器调用时我都无法显示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。
还有其他任何方式让我可以尝试隐藏progressBar,因为用户停止输入文字。
答案 0 :(得分:0)
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);
}