我做了一个自动完成的简单演示,其中每个东西都运行良好。但是当我写第一个字符performFiltering(CharSequence约束)约束给出null值时有一个问题,但之后它给出了正确的值。例如,如果我写“p”它给出null。然后如果我写“pa”它给出约束“pa”..为什么它给第一次空值。 其次如何获取自动完成列表的点击事件(我想获取所选项目的文本) 这是我的代码..
public class CustomAutocompletAdapter extends BaseAdapter implements Filterable {
private String stationNameAndCodeValue;
ArrayList<String> autolistArray;
ArrayList<String> objects;
private Context context;
public CustomAutocompletAdapter(Context context, String[] autolistArray) {
this.autolistArray = new ArrayList<String>();
for (int i = 0; i < autolistArray.length; i++) {
this.autolistArray.add(autolistArray[i]);
}
this.context = context;
}
@Override
public int getCount() {
return autolistArray.size();
}
@Override
public Object getItem(int position) {
return autolistArray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.custom_row_adapter, null);
}
final TextView stationNameAndCode = (TextView) v
.findViewById(R.id.item_selectStationName);
stationNameAndCodeValue = autolistArray.get(position);
stationNameAndCode.setText(stationNameAndCodeValue);
return v;
}
Filter myFilter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
System.out.println("Constraint " + constraint);
Log.d("-----------", "publishResults");
if (results.count > 0 && results != null) {
autolistArray = (ArrayList<String>) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("-----------", "performFiltering");
FilterResults results = new FilterResults();
List<String> filteredArrList = new ArrayList<String>();
if (objects == null) {
objects = new ArrayList<String>(autolistArray);
}
Locale locale = Locale.getDefault();
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = objects.size();
results.values = objects;
} else {
constraint = (String) constraint.toString().toLowerCase(locale);
Pattern logEntry = Pattern.compile("-\\((.*?)\\)");
for (int i = 0; i < objects.size(); i++) {
String name = objects.get(i);
// System.out.println(name);
Matcher matchPattern = logEntry.matcher(name);
String subText = "";
while (matchPattern.find()) {
subText = matchPattern.group(1);
}
if (subText.toLowerCase(locale).contains(constraint)) {
System.out
.println("CustomAutocompletAdapter.myFilter.new Filter() {...}.performFiltering()");
filteredArrList.add(name);
}
}
System.out.println(filteredArrList);
// set the Filtered result to return
results.count = filteredArrList.size();
results.values = filteredArrList;
}
return results;
}
};
@Override
public Filter getFilter() {
return myFilter;
}
}
主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_station);
autocompleteView = (AutoCompleteTextView) findViewById(R.id.item_autoComplete);
STATION_LIST = new String[GlobalList.stationList.length
+ GlobalExtendStationList.stationList.length];
System.arraycopy(GlobalList.stationList, 0, STATION_LIST, 0,
GlobalList.stationList.length);
System.arraycopy(GlobalExtendStationList.stationList, 0,
STATION_LIST, GlobalList.stationList.length,
GlobalExtendStationList.stationList.length);
autosuggestAdapter = new CustomAutocompletAdapter(this,STATION_LIST);
autocompleteView.setAdapter(autosuggestAdapter);
有更新吗?
答案 0 :(得分:0)
此行为是因为AutoCompleteTextView仅在字符数大于阈值时才执行过滤(请参阅source),并且您不能将此阈值分配给小于1.默认情况下,阈值为2.如果1个字符足够为你,致电
autocompleteView.setThreshold(1);
否则,您可以继承AutoCompleteTextView并覆盖setThreshold方法。