FilterQueryProvider, filter and ListView

前端 未结 1 454
深忆病人
深忆病人 2020-12-15 14:32

I have a database as follows:

------------------------------
BOOK NAME | BOOK FORMAT | COUNT |
------------------------------
Android   | HTML       | 1
WPF          


        
相关标签:
1条回答
  • 2020-12-15 15:07

    I think you've messed up things a bit. Actually SimpleCursorAdapter already implements Filterable, so there's no need to reimplement it. Instead in your ListActivity use smth like this:

    private void filterList(CharSequence constraint) {
        final YourListCursorAdapter adapter = 
            (YourListCursorAdapter) getListAdapter();
        final Cursor oldCursor = adapter.getCursor();
        adapter.setFilterQueryProvider(filterQueryProvider);
        adapter.getFilter().filter(constraint, new FilterListener() {
            public void onFilterComplete(int count) {
                // assuming your activity manages the Cursor 
                // (which is a recommended way)
                stopManagingCursor(oldCursor);
                final Cursor newCursor = adapter.getCursor();
                startManagingCursor(newCursor);
                // safely close the oldCursor
                if (oldCursor != null && !oldCursor.isClosed()) {
                    oldCursor.close();
                }
            }
        });
    }
    
    private FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            // assuming you have your custom DBHelper instance 
            // ready to execute the DB request
            return dbHelper.getListCursor(constraint);
        }
    };
    
    0 讨论(0)
提交回复
热议问题