2013-03-24 44 views
0

我在谷歌上搜索和stackover找不到確切的解決方案。 我的問題是,我有一個ArrayList<String>適配器,它有AutoCompleteTextView在適配器中的搜索輸入密鑰<T>

  1. Gatewick英國倫敦
  2. 歐利法國巴黎
  3. 希思羅英國倫敦

如果用戶輸入「經度」爲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); 
+0

有什麼警告? – alex 2013-03-24 02:34:39

+0

顯示java.lang.NullPointerException \t在com.getcontacts.testsil.PkmnAdapter $ PkmnNameFilter.performFiltering(PkmnAdapter.java:57) – 2013-03-24 13:41:28

回答

1

首先,如果您輸入「Lon」,則不應檢查元素是否以「Lon」開頭。也許你需要的if語句切換到:

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

之前你在performFiltering()方法檢查執行任何過濾如果約束爲空(提示:使用TextUtils類)。如果是,則返回原始數據。因此你正在避免NPE。您還需要注意的關鍵點,在那裏NPE可以拋出這樣一個:

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

乾杯,

+0

沒有改變,問題仍然存在 – 2013-03-24 15:49:49

+0

你可以發佈你的代碼所做的更改? – 2013-03-24 17:08:50