使用editText过滤ListView,给出StaleDataException

时间:2013-10-11 07:31:54

标签: android sqlite listview android-cursoradapter

我有这个代码,我在CursorAdapter支持的ListView中过滤结果。

这是databaseHelper方法:

public Cursor getCategoryCursor(String constraints){

    Cursor c = null;
    if(constraints=="")
    c=database.query("categoryTable", null , null, null, null, null, "category_name COLLATE NOCASE ASC");

    else
        c=database.rawQuery("SELECT category_name,_id FROM categoryTable WHERE category_name LIKE '%"+constraints+"%'", null);
    return c;
}

定义此方法后,我在EditText上设置onTextChangeListener,如下所示:

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
    ListView cLV=(ListView)InteractiveVoucher.this.findViewById(R.id.category_list);
    CursorAdapter filterAdapter=(CursorAdapter)cLV.getAdapter();
    filterAdapter.getFilter().filter(arg0.toString());
}

最后,当我运行代码时,我在adapterCalss中的这一行得到了错误:

public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub
    TextView tv=(TextView)view.findViewById(R.id.category_name);
            //get the error on getColumnIndex() line
    tv.setText(mCursor.getString(mCursor.getColumnIndex(mColumnName)));
    tv.setTextSize(20);

0 个答案:

没有答案