2013-03-13 121 views
0

我有一個listview適配器過濾器,正確填寫我的輸入文本使用類似的語句。無法顯示過濾器後的列表視圖數據

我遇到的問題是,當我的應用程序加載時,所有數據都顯示在列表視圖中。當應用過濾器時,它正確地過濾到supplid文本,我不能做的是這樣,一旦edittext再次爲空(即搜索字符串被刪除),所有數據都會在列表視圖中重新顯示。

下面的截圖顯示的問題:

加載列表:

enter image description here

過濾文本 'A'。蘋果按預期顯示:

enter image description here

問題:當「A」被刪除時,列表視圖不重新加載。

enter image description here

代碼過濾器:

 itemNameEdit = (EditText) findViewById(R.id.inputName); 

    showItems.setTextFilterEnabled(true); 

    itemNameEdit.addTextChangedListener(new TextWatcher() 
    { 


     @Override 
     public void afterTextChanged(Editable s) { 

     } 

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

     } 

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

      cursorAdapter.getFilter().filter(s.toString()); 

     } 


    }); 


    getCons = new DBHandlerShop (this, null, null); 

    cursorAdapter.setFilterQueryProvider(new FilterQueryProvider() 
    { 
     public Cursor runQuery(CharSequence constraint) 
     { 

      getCons.open(); 
      return getCons.getChanges(constraint.toString()); 


     } 

    }); 
    showItems.setAdapter(cursorAdapter); 

GetChanges方法:

public Cursor getChanges(String constraintPassed) { 

     String [] columns = new String[]{KEY_ROWSHOPID, KEY_ITEMSHOP, KEY_ITEMNUM, KEY_ITEMPRICE}; 
     Cursor c = null; 
     if(constraintPassed == "" || constraintPassed.length() == 0) 
     { 
      c = ourDatabase.query(DATABASE_TABLESHOP, columns, null, null, null, KEY_ITEMSHOP + " ASC", null); 


     } 

     else 
     { 

      c = ourDatabase.query(DATABASE_TABLESHOP, columns, KEY_ITEMSHOP + " LIKE'" + constraintPassed + "%'", null, null, null, KEY_ITEMSHOP + " ASC", null); 

     } 

     if(c != null) 
      { 
       c.moveToFirst(); 
       //return c; 

      } 
     return c; 
    } 
+0

添加cursorAdapter.notifyDataSetChanged()在textchange方法來刷新列表視圖 – Raghunandan 2013-03-13 18:07:51

+0

你可能不刷新您的列表視圖時EditText上是空白 – Raghunandan 2013-03-13 18:12:29

+0

@Raghunandan我加了DataSetChanged上框TextChanged方法,並將它dosent同樣的問題,刷新 – user1352057 2013-03-13 18:18:48

回答

0

我注意到,您正在使用:

if(constraintPassed == "" || constraintPassed.length() == 0) 

你永遠不應該對於字符串使用==,則必須使用equals()
但在這種情況下,你可以只使用constraintPassed.length() == 0或:

if(constraintPassed.isEmpty()) 

(請閱讀How do I compare strings in Java?學習==equals()之間的差)