回调完成后从语句返回

时间:2017-03-28 19:48:35

标签: java android callback

基本上我正在寻找一种聪明的方法让dbInteractionComplete中的回调运行完毕后让函数返回。

目前,该函数显然在回调完成之前返回。也许有一种聪明的方法让回报等到回调结束?

private List<Post> toggle() {

    final List<Post> postList = new ArrayList<>();
    final QuestionnaireWrapper mCurrentQuestionnaireWrapper = DatabaseHelper.getCurrentQuestionnaire(teamId);
    if (mCurrentQuestionnaireWrapper != null) {
        if (mCurrentQuestionnaireWrapper.isCompleted()) {
            final QuestionnaireFeedbackDialog mQuestionnaireFeedbackDialog = QuestionnaireFeedbackDialog.newInstance(mCurrentQuestionnaireWrapper.getQuestionnaire().getFeedback());
            mQuestionnaireFeedbackDialog.show(getChildFragmentManager(), "questionnaire_feedback");
            final String uniqueId = teamId + "," + mCurrentQuestionnaireWrapper.getRemoteId();
            DatabaseHelper.deleteQuestionnaire(teamId, uniqueId, new DBCallbackInterface() {
                @Override
                public void dbInteractionComplete() {
                    final QuestionnaireWrapper mNextCurrentQuestionnaire = DatabaseHelper.getCurrentQuestionnaire(teamId);
                    if (mNextCurrentQuestionnaire != null) {
                        mCurrentQuestionnaire = mNextCurrentQuestionnaire.getQuestionnaire();
                        final Post post = new Post();
                        post.setType(Questionnaire.QUESTIONNAIRE_FEED_TYPE);
                        postList.add(post);
                    }
                }
            });
            return postList;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

  

在回调完成后从一个语句返回Java android

通常回调接口用于异步执行,因此您不应等到回调完成。在android中阻止UI线程也可能导致应用程序崩溃。

答案 1 :(得分:0)

创建一个界面: public interface SomeInterface { void callback (List<Post>) ;}

创建一个实现DBCallbackInterface的类:

    public static class SomeNewClass implements DBCallbackInterface {

    private List<Post> list;

    private SomeInterface iAmAwsome;
    private int teamId;
    public SomeNewClass(List<Post> list, SomeInterface iAmAwsome, int teamId) {

    this.list = list;

    this.iAmAwsome = iAmAwsome;

    this.teamId = teamId;
    }

    @Override
     public void dbInteractionComplete() {
final QuestionnaireWrapper mNextCurrentQuestionnaire = DatabaseHelper.getCurrentQuestionnaire(teamId);
  if (mNextCurrentQuestionnaire != null) {
                                mCurrentQuestionnaire = mNextCurrentQuestionnaire.getQuestionnaire();
                                final Post post = new Post();
                                post.setType(Questionnaire.QUESTIONNAIRE_FEED_TYPE);              list.add(post);

    if (iAmAwsome! = null) 
iAmAwsome.callback(list) ;
    }
    }

用法:在你的切换方法中:

DatabaseHelper.deleteQuestionnaire(teamId, uniqueId,  new SomeNewClass(postList, new SomeInterface() {
@Override
                public void callback(List<Post>yourdata){code here } }, teamId)