2015-05-19 38 views
0

我的代码如下用于搜索过滤列表视图。每次修改tbSearch editText中的文本时,都必须更改listview中的项目。执行进入if语句(txt.length()==0),但它不会添加我的数组。ArrayAdapter addAll函数似乎没有被调用

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    String txt = tbSearch.getText().toString().toLowerCase(); 
    aaItems.clear(); 
    if (txt.length() == 0) 
    { 
     aaItems.addAll(arrMonth); 
    } 
    else { 
     for (String item : arrMonth) { 
      if (item.toLowerCase().contains(txt)) { 
       aaItems.add(item); 
      } 
     } 
    } 
} 
+1

您是否在适配器上通过了“notifyDataSetChanged”? – tachyonflux

+0

看看这个答案 - http://stackoverflow.com/questions/5125350/android-arrayadapter-add-method-not-working – Zain

+0

notifyDataSetChanged不适用于我。 –

回答

2

要传递到您的ArrayAdapter列表作为适配器的后盾的ArrayList。

当你调用clear()时,你实际上清除了你给它的后备数组。

你需要确保你给的适配器列表比你arrMonth列表不同,像这样:

aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, 
    new ArrayList<String>(arrMonths)); 
+0

我试图创建一个备份ArrayList,但它仍然被清除。 –

+0

所以你创建了一个无法修改的最终对象。但是,如果我的ArrayList将来自数据库呢?这意味着我不能使用这样的固定列表。 –

+0

这很好。只是不要使用相同的ArrayList来将所有可能的值保存为传递给ArrayAdapter的值。 –

0

好吧,我解决了这个问题@迈克尔克劳斯的帮助。多谢兄弟!我复制了temp的另一个arraylist,并在textchangedlistener中使用。

public class ItemList extends Activity { 

    ListView lvItems; 
    EditText tbSearch; 
    //String[] arrMonth = {"January","February","March","April","May","June","July","August","September","October","November","December"}; 
    ArrayList<String> arrMonth = new ArrayList<>(); 
    ArrayList<String> temp = new ArrayList<>(); 
    ArrayAdapter<String> aaItems; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_item_list); 
     lvItems = (ListView) findViewById(R.id.lvItems); 
     tbSearch = (EditText) findViewById(R.id.tbSearch); 
     PopulateArrayList(); 
     temp.addAll(arrMonth); 

     aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrMonth); 
     lvItems.setAdapter(aaItems); 

     tbSearch.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) { 
       String txt = tbSearch.getText().toString().toLowerCase(); 
       aaItems.clear(); 
       if (s.length() == 0) { 
        aaItems.addAll(temp); 
       } else { 
        for (String item : temp) { 
         if (item.toLowerCase().contains(txt)) { 
          aaItems.add(item); 
         } 
        } 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 

    } 

    public void btnAdd_Click(View v) 
    { 
     aaItems.add("Patrick"); 
    } 

    public void PopulateArrayList() 
    { 
     arrMonth.add("January"); 
     arrMonth.add("February"); 
     arrMonth.add("March"); 
     arrMonth.add("April"); 
     arrMonth.add("May"); 
     arrMonth.add("June"); 
     arrMonth.add("July"); 
     arrMonth.add("August"); 
     arrMonth.add("September"); 
     arrMonth.add("October"); 
     arrMonth.add("November"); 
     arrMonth.add("December"); 
    } 
} 
+0

酷,很高兴它为你工作帕特里克。我有两个要求:如果我的回答对你有帮助,你能否接受我的回答?其次,版主可能会回答你自己的问题,而不是修改你原来的问题。仅供参考。 :) –

+0

@MichaelKrause:回答你自己的问题完全没问题。这里主要的一点是看它是否真的回答了这个问题。 – Makoto

+0

@Makoto:谢谢 - 很高兴知道。 –