我正在尝试为我的AutoCompleteTextView设置一个过滤器,如果键入的单词与HashMap中的单词匹配,则会显示建议。问题是,当我尝试在getView()中填充View时,我得到错误java.lang.IndexOutOfBoundsException。如果我手动在getView()中给出一个位置,我从HashMap中获取一个相应的值,但是我无法选择它。
Map<String,List<String>> countries = new HashMap<String,List<String>>();
AutoCompleteAdapter adapter = new AutoCompleteAdapter(this,words);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.txtCountries);
textView.setThreshold(1);
textView.setAdapter(adapter);
适用于适配器:
public class AutoCompleteAdapter extends BaseAdapter implements Filterable {
List<String>testing = new ArrayList<String>();
private Context mContext;
private Map<String,List<String>> mWords;
public AutoCompleteAdapter(Context context,Map<String,List<String>> words) {
mContext = context;
mWords = words;
}
@Override
public int getCount() {
return mWords.size();
}
@Override
public Object getItem(int position) {
return mWords.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.auto_complete_text_view,parent,false);
}
//Error is caused here by position
Log.d("Letter", String.valueOf(position));
TextView textView = (TextView) convertView.findViewById(R.id.text1);
textView.setText(testing.get(position));
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint!=null) {
List<String> wordList = null;
if (mWords.containsKey(constraint)) {
wordList = new ArrayList<>();
wordList.addAll(mWords.get(constraint));
}
filterResults.values = wordList;
filterResults.count = wordList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results !=null && results.count>0){
notifyDataSetChanged();
testing = (ArrayList<String>)results.values;
}else{
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
答案 0 :(得分:0)
原来我在getCount()和getItem()方法中有错误的列表。它知道与HashMap完美配合。
这是最后的
public class AutoCompleteAdapter extends BaseAdapter implements Filterable {
List<String>testing = new ArrayList<String>();
private Context mContext;
private Map<String,List<String>> mWords;
public AutoCompleteAdapter(Context context,Map<String,List<String>> words) {
mContext = context;
mWords = words;
}
@Override
public int getCount() {
return testing.size();
}
@Override
public Object getItem(int position) {
return testing.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.auto_complete_text_view,parent,false);
}
if(position<testing.size()) {
TextView textView = (TextView) convertView.findViewById(R.id.text1);
textView.setText(testing.get(position));
}
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint!=null) {
List<String> wordList = null;
if (mWords.containsKey(constraint)) {
wordList = new ArrayList<>();
wordList.addAll(mWords.get(constraint));
}
filterResults.values = wordList;
filterResults.count = wordList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results !=null && results.count>0){
notifyDataSetChanged();
testing = (ArrayList<String>)results.values;
}else{
notifyDataSetInvalidated();
}
}
};
return filter;
}
}