how to refresh the listView using the Cursor Adapter

前端 未结 6 2326
名媛妹妹
名媛妹妹 2021-01-05 07:56

I have created a ListView using CursorAdapter . Now I am Trying to update the ListView and Refresh the value to the ListView .

6条回答
  •  粉色の甜心
    2021-01-05 08:15

    In your CursorDemo you have to owerwrite changeCursor() method and reset the Cursor if you have indexer you have to set it's cursor too.

    @Override
    public void changeCursor(Cursor cursor) {
        mIndexer.setCursor(cursor);
        super.changeCursor(cursor);
    }
    

    public void changeCursor (Cursor cursor)

    Added in API level 1 Change the underlying cursor to a new cursor. If there is an existing cursor it will be closed.

    Parameters cursor The new cursor to be used

    Also try for below method if it's apt for your requirement.

    Set a FilterQueryProviderand pass your key to that filter.

      final Cursor oldCursor = adapter.getCursor();
        adapter.setFilterQueryProvider(myQueryProvider);
        adapter.getFilter().filter(editTextValue, 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 myQueryProvider = new FilterQueryProvider() {
            public Cursor runQuery(CharSequence searchKey) {
                // assuming you have your custom DBHelper instance 
                // ready to execute the DB request
                return sqlDataSore.updateData(searchKey);;
            }
        };
    

    PS : The Cursor must include a column named _id or this class will not work see this.

提交回复
热议问题