2015-05-19 61 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:謝謝 - 很高興知道。 –