简体   繁体   中英

does a method implementing a callBack (onSuccess()) return the value returned by onSucess()?

I override method getView() and I'm trying to get the view inflated inside a onSuccess() method that return the new view. The value returned by onSuccess is it returned by getView() ?

Here is my code :

    @Override 
    public View getView(final int position, final View view,final ViewGroup parent) {

     aMethodWithAcallBack(new myCallBack(){

        @Override
        public void onSuccess(ArrayList<Content> contents) {

            contentsList = contents ;
            View mView = inflateIfRequired(view, position, parent);

            if(getItem(position).content_type_id == 2) {
                 bindCard(view, getItem(position));
            }
           else {
              if(getItem(position).content_type_id == 3)
                 bindVideo(view,getItem(position)) ;
           }

           return mView ;
      });


      @override
      public void onFail() {
        //
      }

      return view ;
   } 

You may be wondering why I used a CallBack , I have to use it because the contentsList items are the items of my ListView , and this contentsList is initiated thanks to a callBack because it's elements are fetched from a server.

No, it cannot be done by returning the value from onSuccess(). Since there would be no main thread to receive the value returned by onSuccess() method.

Explanation:

1) getView() method is invoked from other place - This is one thread let us keep it as main.

2) In the flow of execution of this main thread an async callback method aMethodWithAcallBack() is executed, this async callback call will be a different thread.

3) Main thread which is executing the getView() method will pass thru and exit the method by executing return view ;

4) Once async callback is success, the line return mView ; will be executed but there is no main thread to receive it. ie we would not have the main thread which had invoked the getView() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM