我正在使用AutoCompleteTextView创建一个Deck Building应用程序,在我的一个用户报告它无法正常工作之前,一切似乎都没问题。
视图应根据提供的卡片名称列表自动完成,但似乎并未显示所有相关选项。
例如,视图中包含以下名称
" Sweet Apple Acres的水浸" "保存甜苹果英亩" "甜苹果 亩" "甜蜜和善良"
打字" Sweet"进入视图将显示所有这些值,但键入" Sweet" (带尾随空格)或" Sweet A"只返回结果" Sweet Apple Acres"和" Sweet And Kind"即使所有其他选项都有效。
如果文本字段中有空格,则似乎忽略任何不以给定文本开头的自动完成选项。有没有办法改变这个?我使用默认设置使用基本的AutoCompleteTextView。
AutoCompleteTextView autoText = (AutoCompleteTextView) view.findViewById(R.id.card_name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_dropdown_item_1line, CardDatabase.CARD_NAMES);
autoText.setAdapter(adapter);
答案 0 :(得分:2)
您可以使用自定义适配器
public class CustomAdapter extends BaseAdapter implements Filterable {
private Context context;
private List<String> originalData;
private List<String> filteredData;
private ItemFilter mFilter = new ItemFilter();
public CustomAdapter(List<String> itemList, Context ctx) {
originalData = new ArrayList<String>();
filteredData = new ArrayList<String>();
filteredData = itemList;
originalData = itemList;
context = ctx;
}
@Override
public int getCount() {
return filteredData.size();
}
@Override
public Object getItem(int position) {
return filteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(android.R.layout.simple_dropdown_item_1line, null);
}
final TextView text = (TextView) convertView.findViewById(android.R.id.textView);
text.setText(filteredData.get(position));
return convertView;
}
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
Filter.FilterResults results = new FilterResults();
final List<String> list = originalData;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString;
for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().startsWith(filterString)) {
nlist.add(filterableString);
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredData = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
答案 1 :(得分:-1)