适配器<t> </t>中的AutoCompleteTextView搜索输入键

时间:2013-03-24 01:33:56

标签: android android-arrayadapter autocompletetextview

我在google上搜索并且stackover无法找到确切的解决方案。 我的问题是,我有一个ArrayList<String>适配器,它有

  1. 英国伦敦Gatewick
  2. Ory Paris France
  3. 希思罗机场伦敦英格兰
  4. 如果用户输入&#34; Lon&#34;进入AutoCompleteTextView然后我必须显示第1行和第3行。因为这些都有伦敦字符串。

    我尝试了this link并在此处粘贴了代码,但它在第57行上发出了警告

    String prefix = constraint.toString().toLowerCase();
    

    PkmnAdapter

    public class PkmnAdapter extends ArrayAdapter<String> {
    
        private ArrayList<Pkmn> original;
        private ArrayList<Pkmn> fitems;
        private Filter filter;
    
        public PkmnAdapter(Context context, int textViewResourceId,
                ArrayList<Pkmn> items) {
            super(context, textViewResourceId);
            this.original = new ArrayList<Pkmn>(items);
            this.fitems = new ArrayList<Pkmn>(items);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            Pkmn pkmn = fitems.get(position);
            if (pkmn != null) {
                TextView tt = (TextView) v.findViewById(R.id.RlabPName);
    
                if (tt != null) {
                    tt.setText(pkmn.getNAME());
                }
            }
            return v;
        }
    
        @Override
        public Filter getFilter() {
            if (filter == null)
                filter = new PkmnNameFilter();
    
            return filter;
        }
    
        private class PkmnNameFilter extends Filter {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                String prefix = constraint.toString().toLowerCase();
    
                if (prefix == null || prefix.length() == 0) {
                    ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                    results.values = list;
                    results.count = list.size();
                } else {
                    final ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                    final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>();
                    int count = list.size();
    
                    for (int i = 0; i < count; i++) {
                        final Pkmn pkmn = list.get(i);
                        final String value = pkmn.getNAME().toLowerCase();
    
                        if (value.startsWith(prefix)) {
                            nlist.add(pkmn);
                        }
                    }
                    results.values = nlist;
                    results.count = nlist.size();
                }
                return results;
            }
    
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                fitems = (ArrayList<Pkmn>) results.values;
    
                clear();
                if (fitems != null) {
                    int count = fitems.size();
                    for (int i = 0; i < count; i++) {
                        Pkmn pkmn = (Pkmn) fitems.get(i);
                        fitems.add(pkmn);
                    }
                }
            }
    
        }
    }
    

    MainActivity.java 以放置适配器

    Pkmn[] item = new Pkmn[4];
    item[0] = new Pkmn("Gatewick London England");
    item[1] = new Pkmn("Ory Paris France");
    item[2] = new Pkmn("Heathrow London England");
    item[3] = new Pkmn("Ataturk Istanbul Turkey");
    
    ArrayList<Pkmn> list = new ArrayList<Pkmn>(Arrays.asList(item));
    MultiAutoCompleteTextView auto = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
    PkmnAdapter adap = new PkmnAdapter(this,android.R.layout.simple_list_item_1, list);
    

1 个答案:

答案 0 :(得分:1)

首先,如果您输入“Lon”,则不应检查元素是否以“Lon”开头。您可能需要将if语句切换为:

 if (value.contains(prefix)) {
         nlist.add(pkmn);
 }

performFiltering()方法中执行任何过滤之前,请检查约束是否为空(提示:使用TextUtils类)。如果是,则返回原始数据。因此,您正在避免NPE。你还需要注意NPE可以像这样抛出的关键点:

 if (prefix == null || prefix.length() == 0) {   }

干杯,