2012-01-04 68 views
1

我創建了一個過濾器列表視圖,其中用戶將通過edittext輸入輸入,並且結果在列表視圖中被過濾。Android的listview edittext過濾器空間按鈕?

代碼運行良好,但是當我按下空格按鈕時,列表視圖中的結果消失。如何在不打開listview內容的情況下在搜索輸入中添加空間?

這裏是我當前的代碼:

myListview.setTextFilterEnabled(true); 

    edNearBy.addTextChangedListener(new TextWatcher() { 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 

     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

      NearByActivity.this.aa.getFilter().filter(s); 

     } 
    }); 

回答

3

做到這一點的方法:

public void afterTextChanged(Editable s) { 
    String st = s.toString().trim();    
    NearByActivity.this.aa.getFilter().filter(st); 
} 

但不要做過濾這種方式,here is the correct one

+2

日Thnx完美地工作,U 3線錯過 「ST」 壽 NearByActivity.this.aa.getFilter()過濾器(ST); – ZealDeveloper 2012-01-04 06:22:50

+1

:D修正了.. – 2012-01-04 06:31:50

1

我發現溶液:

String[] hi={"Hi there", "Hello", "Bye"}; 

adapter = new ArrayAdapter<String>(slownik.this, R.layout.list_item, R.id.word_name,hi); 
lv.setAdapter(adapter); 

inputSearch.addTextChangedListener(new TextWatcher() { 

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

     lv.setTextFilterEnabled(true); 
     lv.setFilterText(s.toString().trim()); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 


    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     slownik.this.adapter.getFilter().filter(s.toString().trim()); 
     if(s.length()==0){ 
      lv.clearTextFilter(); 
     } 
    } 
});