问题描述:Autocompletetextview返回的非空参数为空。
解决方法:
String[] data = {"Apple", "Banana", "Cherry"};
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, data);
AutocompleteTextView autocompleteTextView = findViewById(R.id.autocompleteTextView);
autocompleteTextView.setAdapter(adapter);
autocompleteTextView.setThreshold(1); // 设置输入多少个字符后开始过滤,默认为2个字符
autocompleteTextView.setFilter(new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList tempList = new ArrayList<>();
// 执行过滤逻辑,并将结果存储在tempList中
for (String item : data) {
if (item.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
tempList.add(item);
}
}
filterResults.values = tempList;
filterResults.count = tempList.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// 更新Autocompletetextview的数据源
adapter.clear();
adapter.addAll((ArrayList) results.values);
adapter.notifyDataSetChanged();
} else {
autocompleteTextView.setText(""); // 清空Autocompletetextview的文本
}
}
});
autocompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
// 处理非空参数为空的情况
}
}
});
通过以上方法,你可以解决Autocompletetextview返回的非空参数为空的问题。请根据实际情况选择适用的解决方法。