如何根据布尔变量确定导航抽屉片段的视图?

问题描述 投票:0回答:2

我有问题,我不知道如何解决。我正在为自己学习Android,现在正在使用导航抽屉与应用程序的一部分进行交互。我想要的是从questionItem中选择NavigationActivity时要根据Firebase Firestore中存储的布尔变量返回View的方法。我知道Firebase上的查询是异步的,这就是为什么即使存储的变量的值为true也总是返回no_question布局的原因。但是我不知道如何解决,如果有人帮助我找到解决方案,我将非常感激。我已经尝试了许多解决方案,例如使用线程同步或休眠主线程,但是它不起作用。

这是我的代码:

public class QuestionFragment extends Fragment {

    private View root;

    private boolean[] question_act = new boolean[1];

    public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

        isQuestion_act(new Callback() {
            @Override
            public void activacioRebuda(final boolean qNova) {
                question_act[0] = qNova;
            }
        });

        if (question_act[0]) root = inflater.inflate(R.layout.fragment_question, container, false);
        else root = inflater.inflate(R.layout.fragment_noquestion, container, false);

        return root;
    }

    public interface Callback {
        void activacioRebuda(final boolean question);
    }

    private void isQuestion_act(Callback callback) {

            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            assert user != null;
            String id = user.getUid();

            FirebaseFirestore db = FirebaseFirestore.getInstance();

            db.collection("players")
                    .whereEqualTo("Id", id).whereEqualTo("Question_act", true)
                    .get()
                    .addOnCompleteListener(task -> {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                                boolean q = document.getBoolean("Question_act");
                                Log.d(TAG, " question " + q);
                                if (callback != null) {
                                    callback.activacioRebuda(q);
                                }
                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                            Toast.makeText(getActivity(), "User does not exist", Toast.LENGTH_SHORT).show();
                        }
                    });
    }
}
android firebase android-fragments google-cloud-firestore navigation-drawer
2个回答
0
投票

当您在setNavigationItemSelectedListener中选择一个片段时,

将布尔值放入包中

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putBoolean(key, value);
fragment.setArguments(bundle);

然后在您的片段中,使用以下方法检索数据(例如,在onCreate()方法中):

Bundle bundle = this.getArguments();
if (bundle != null) {
        boolean myBoolean= bundle.getBoolean(key, defaultValue);
}

您可以使用myBoolean来确定要用于此片段的布局。


0
投票

仅分配question_act[0] = qNova;没有任何区别。正如您在问题中已经提到的那样,Firebase API是异步的,因此在回调之外使用qNova的值将never有效。

您唯一的选择是仅在qNova方法内部使用activacioRebuda()的值来等待数据,因为您非常确定从数据库获取数据的操作已100%完成。

有关有效的解决方案,请从以下帖子中查看我的答案:

© www.soinside.com 2019 - 2024. All rights reserved.