如何在android上等待asynchronus方法

时间:2016-06-22 21:54:31

标签: android twitter-fabric

我有这个班级与twitter连接

public class TwitterConnect {
    TwitterApiClient twitterApiClient;
    StatusesService statusesService;
    Long [] ids;

    public TwitterConnect(){
        twitterApiClient = TwitterCore.getInstance().getApiClient();
        statusesService = twitterApiClient.getStatusesService();
        callHomeTimeLine();
    }

    private void callHomeTimeLine(){

        statusesService.homeTimeline(200, null, null, null, null, null, null, new Callback<List<Tweet>>() {
            @Override
            public void success(Result<List<Tweet>> result) {
                Long [] ids = new Long[result.data.size()];
                int i=0;

                for (Tweet tweet:result.data) {
                    //tweet.id;
                    Log.w("Twwet",tweet.id+" ");
                    ids[i] = result.data.get(i).id;
                    i++;
                }
                setIds(ids);
            }

            @Override
            public void failure(TwitterException exception) {
                Log.e("FAILURE", "Fallo en callHomeTimeLine");
            }
        });

    }

   private void setIds(Long [] ids){
     this.ids = ids;
   }

    public Long[] getIds() {
        return ids;
    }
}

我在活动中使用这个clas

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_following);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        TwitterConnect twc = new TwitterConnect();
        this.ids = twc.getIds()//ids its always null
;

但是this.ids总是为null,在succces方法上它是异步的,我怎么能等待呢?在edbug模式下,数据被正确检索,但活动在异步方法完成之前使用了ids数组。

1 个答案:

答案 0 :(得分:1)

“等待”异步任务完成的常用方法是(在您的情况下),如下所示:

在您的TwitterConnect类中,添加一个界面:

interface Idsinterface {
    void setIds(Long[] ids);
}

并将其作为参数传递给TwitterConnect Constructor,它看起来像这样:

public class TwitterConnect {
    TwitterApiClient twitterApiClient;
    StatusesService statusesService;
    Long [] ids;

    public TwitterConnect(IdsInterface idsInterface){
        twitterApiClient = TwitterCore.getInstance().getApiClient();
        statusesService = twitterApiClient.getStatusesService();
        callHomeTimeLine(idsInterface);
    }

    private void callHomeTimeLine(IdsInterface idsInterface){

        statusesService.homeTimeline(200, null, null, null, null, null, null, new Callback<List<Tweet>>() {
            @Override
            public void success(Result<List<Tweet>> result) {
                Long [] ids = new Long[result.data.size()];
                int i=0;

                for (Tweet tweet:result.data) {
                    //tweet.id;
                    Log.w("Twwet",tweet.id+" ");
                    ids[i] = result.data.get(i).id;
                    i++;
                }
                idsInterface.setIds(ids);
            }

            @Override
            public void failure(TwitterException exception) {
                Log.e("FAILURE", "Fallo en callHomeTimeLine");
            }
        });

    }

    private void setIds(Long [] ids){
        this.ids = ids;
    }

    public Long[] getIds() {
        return ids;
    }

    interface IdsInterface {
        void setIds(Long[] ids);
    }
}

现在您可以像这样使用此类:

new TwitterConnect(TwitterConnect.IdsInterface() {
       @Override
       void setIds(Long[] ids) {
           YourActivity.this.ids = ids;
           //rest of your code here
    });