我使用简单的游标适配器根据编辑文本中输入的文本过滤列表视图项。我不能这样做。代码中没有错误。但过程即过滤不会发生。列表视图仍然没有任何变化。任何人都可以帮助我吗?
这是我的代码。 >
SimpleCursorAdapter sca;
ListView lv;
EditText searchtext;
lv = (ListView) findViewById(R.id.listView1);
searchtext = (EditText) findViewById(R.id.eT_sth);
searchtext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
sca.getFilter().filter(arg0);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
sca.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence cse) {
String pi=null;
if(cse!=null)
pi=cse.toString();
return db.filteredItem(pi);
}
});
public Cursor filteredItem(String st) throws SQLException {
Cursor mcursor = null;
SQLiteDatabase db = this.getReadableDatabase();
try {
if (st == null || st.length() == 0) {
String query = "SELECT * FROM" + this.TABLE_MIC2 + "";
mcursor = db.rawQuery(query, null);
} else {
mcursor = db.query(this.TABLE_MIC2, new String[] { this.KEY_ID,
this.KEY_ITEMNUMBER, this.KEY_DESCRIPTION,
this.KEY_PICKINGSEQUENCE, this.KEY_QTYONHAND,
this.KEY_QTYCOUNTED, this.KEY_STOCKUNIT,
this.KEY_STATUS }, this.KEY_ITEMNUMBER + " like '%"
+ st + "%'", null, null, null, null);
}
if (mcursor != null) {
mcursor.moveToFirst();
}
} catch (Exception e) {
e.printStackTrace();
}
return mcursor;
}
这是我的listview方法。
public void inserttolist(String SpnLocation) {
try {
cu = db.getdata(SpnLocation);
String[] columns = new String[] { DatabaseHandler.KEY_ITEMNUMBER,
DatabaseHandler.KEY_DESCRIPTION,
DatabaseHandler.KEY_PICKINGSEQUENCE,
DatabaseHandler.KEY_QTYONHAND,
DatabaseHandler.KEY_QTYCOUNTED,
DatabaseHandler.KEY_STOCKUNIT, DatabaseHandler.KEY_STATUS };
int[] to = new int[] { R.id.text_itemnumber, R.id.text_desc,
R.id.text_ps, R.id.text_qoh, R.id.text_qc, R.id.text_su,
R.id.text_st };
sca = new SimpleCursorAdapter(this, R.layout.list_row, cu, columns,
to, 0);
lv.setAdapter(sca);
} catch (Exception ex) {
Log.e("Failed", ex.getLocalizedMessage());
}
}