[英]Unable to display listview data after filter
我有一個listview適配器過濾器,它使用like語句正確過濾了我輸入的文本。
我遇到的問題是,當我的應用加載時,所有數據都顯示在列表視圖中。 應用過濾器后,它會正確過濾提供的文本, 我無法做的是,一旦edittext再次為空(即刪除搜索字符串),所有數據將重新顯示在列表視圖中。
以下屏幕截圖顯示了該問題:
載入清單:
過濾文本“ a”。 蘋果按預期顯示:
問題:刪除“ a”后,不會重新加載列表視圖。
過濾器代碼:
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;
}
我注意到您正在使用:
if(constraintPassed == "" || constraintPassed.length() == 0)
您永遠不要對字符串使用==
,而必須使用equals()
。
但是在這種情況下,您可以只使用constraintPassed.length() == 0
或:
if(constraintPassed.isEmpty())
(請閱讀如何在Java中比較字符串?以了解==
和equals()
之間的區別。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.