2017-08-29 28 views
0

这是来自主要活动类。我有一个数组ArrayList,需要为listview创建一个搜索栏。如何在android studio中包含对我的自定义列表视图的搜索?

adapter = new ArrayAdapter<String[]>(this, R.layout.list_view, android.R.id.text1, spellList) 
    { 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      View view = super.getView(position, convertView, parent); 

      String[] entry = spellList.get(position); 
      TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
      TextView text2 = (TextView) view.findViewById(android.R.id.text2); 
      text1.setText(entry[0]); 
      text2.setText(entry[1]); 

      return view; 
     } 
    }; 
    wizList.setAdapter(adapter); 

    searchText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 


     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 

这是字符串数组的Arraylist中的数据。如果按拼写名称搜索,则首选。

final ArrayList<String[]> spellList; 

public WizardDatabase() 
{ 
    spellList = new ArrayList<>(); 

    spellList.add(new String[] { "Resistance", "Subject gains +1 on saving throws."}); 
    spellList.add(new String[] { "Acid Splash", "Orb deals 1d3 acid damage."}); 
    spellList.add(new String[] { "Detech Poison", "Detects poison in one creature or small object."}); 

public ArrayList<String[]> getList() 
{ 
    return spellList; 
} 

谢谢。

回答

0

如果是简单的字符串搜索才使用下面的代码

searchText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 


     @Override 
     public void afterTextChanged(Editable s) { 
      if (s.length() > 3) { // it's for performance I've put 3 you can change it 
       adapter.getFilter().filter(s.toString()); 
      } 
     } 
    }); 
+0

过滤器对我的字符串数组的数组列表不起作用。这主要是问题所在。 –

+0

@GarryAlcorn那么你需要实现你自己的过滤器版本 –

+0

我需要创建一个自定义适配器吗? –

相关问题