0

我在谷歌上搜索,stackover 找不到确切的解决方案。我的问题是,我有一个ArrayList<String>适配器,它有

  1. 盖特威克伦敦英格兰
  2. Ory 巴黎 法国
  3. 英国伦敦希思罗机场

如果用户输入“Lon”,AutoCompleteTextView那么我必须显示第 1 行和第 3 行。因为它们有伦敦字符串。

我尝试了此链接并在此处粘贴了代码,但它在第 57 行给出了警告

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

Pkmn适配器

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);
4

1 回答 1

1

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

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

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

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

干杯,

于 2013-03-24T13:50:58.197 回答