Implementing AsyncQueryHandler

和自甴很熟 提交于 2019-11-26 22:55:18

问题


I am trying to implement AsyncQueryHandler because I am experiencing the same exact problem in this link but I dont see any example or anything about implementing it.

I tried doing

AsyncQueryHandler handler = new AsyncQueryHandler(getContentResolver());

since that is what the constructor shows in the docs but I get an error saying cannot instantiate the type AsyncQueryHandler so how do I use it then?


回答1:


AsyncQueryHandler is an abstract class, thus you cannot instantiate it directly. You need to subclass it.




回答2:


It's so convenient:

AsyncQueryHandler handler = 
    new AsyncQueryHandler(mActivity.getContentResolver()) {};
handler.startUpdate(-1, null, sessionUri, values, null, null);



回答3:


It comes very handy when you need to do CRUD operationa async without using a Loader, so by extending from asyncquery you get method such as startQuery, startInsert, startUpdate and startDelete an easy implementation could be:

public class CustomAsyncHandler extends AsyncQueryHandler {

        private WeakReference<AsyncQueryListener> mListener;

        public interface AsyncQueryListener {
            void onQueryComplete(int token, Object cookie, Cursor cursor);
        }

        public CustomAsyncHandler(ContentResolver cr, AsyncQueryListener listener) {
            super(cr);
            mListener = new WeakReference<AsyncQueryListener>(listener);
        }

        public CustomAsyncHandler(ContentResolver cr) {
            super(cr);
        }

        /**
         * Assign the given {@link AsyncQueryListener} to receive query events from
         * asynchronous calls. Will replace any existing listener.
         */
        public void setQueryListener(AsyncQueryListener listener) {
            mListener = new WeakReference<AsyncQueryListener>(listener);
        }

        /** {@inheritDoc} */
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            final AsyncQueryListener listener = mListener.get();
            if (listener != null) {
                listener.onQueryComplete(token, cookie, cursor);
            } else if (cursor != null) {
                cursor.close();
            }
        }

    }

As you can see there is an extra interface AsyncQueryListener created to be as a callback when query is completed, as this you can create AsyncUpdateListener, AsyncDeleteListener, AsyncInsertListener, and so on.




回答4:


 AsyncQueryHandler asyncQueryHandler = new AsyncQueryHandler(activity.getContentResolver()){

        @Override
        protected void onInsertComplete(int token, Object cookie, Uri uri) {
            super.onInsertComplete(token, cookie, uri);
            Log.d(TAG, "onInsertComplete  return uri: " + uri);
        }

 };

 asyncQueryHandler.startInsert(-1, null, mUri, values);


来源:https://stackoverflow.com/questions/11961857/implementing-asyncqueryhandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!