简体   繁体   English

如何在代码中的确切位置将数据从 firebase 提取到 android studio?

[英]How do I pull data from firebase into android studio at the exact point in the code?

Is there any way to pull data from firebase where the code is?有没有办法从代码所在的firebase中提取数据? I currently have valueEventListeners, but they all run after the code below them, thus invalidating the following code.我目前有 valueEventListeners,但它们都在它们下面的代码之后运行,从而使以下代码无效。 I want to be able to pull a value exactly where the code is, not later.我希望能够在代码所在的位置提取一个值,而不是稍后。

As of yet, I have not found anything online about this.到目前为止,我还没有在网上找到任何关于此的信息。

A good example of my problems in the code:我在代码中遇到的问题的一个很好的例子:

public void onItemClick(AdapterView<?> l, View v, final int position, long id) {

        FirebaseAuth mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        final String uid = user.getUid();

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                cCESnapshot = dataSnapshot.child(uid).child("currChallenges").child(challengeList.get(position));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        Intent intent = new Intent();
        intent.setClass(this, ChallengeView.class);
        intent.putExtra("snapshot", cCESnapshot.toString());
        intent.putExtra("name", challengeList.get(position));
        startActivity(intent);
    }

cCESnapshot is null because the intent runs before the valueEventListener. cCESnapshot 为空,因为意图在 valueEventListener 之前运行。

The onDataChange() is asynchronous, so the only way to use the retrieved data is inside onDataChange() , example: onDataChange()是异步的,因此使用检索到的数据的唯一方法是在onDataChange() ,例如:

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            cCESnapshot = dataSnapshot.child(uid).child("currChallenges").child(challengeList.get(position));

    Intent intent = new Intent();
    intent.setClass(this, ChallengeView.class);
    intent.putExtra("snapshot", cCESnapshot.toString());
    intent.putExtra("name", challengeList.get(position));
    startActivity(intent);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

This is an asynchronous operation, when the data will arrive, the onDataChange callback will be triggered.这是一个异步操作,当数据到达时,将触发onDataChange回调。 Your startActivty code will be executed sequentially and this is why you get no value in cCESnapshot .您的startActivty代码将按顺序执行,这就是您在cCESnapshot没有任何价值的cCESnapshot

Move the startActivity code inside the listener.在侦听器内移动startActivity代码。

But be careful , because each time the onItemClick click listener will be called, you'll add a value event listener.但是要小心,因为每次调用onItemClick单击侦听器时,都会添加一个值事件侦听器。 That way, you'll have multiple calls to onDataChange in each click and so multiple startActivities .这样,您将在每次单击中多次调用onDataChange以及多个startActivities

Instead, i recommend using addListenerForSingleValueEvent which will be triggered only after a single change in data.相反,我建议使用addListenerForSingleValueEvent ,它只会在数据发生一次更改后触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio:如何从 Firebase 检索特定数据? - Android studio : How do I retrieve specific data from Firebase? 当我在Android Studio上从github提取代码时,发生代码更改问题? - Issue with code changing when I pull from github on Android Studio? 如何将Android Studio中的模块(而非项目)链接到Firebase? 与将应用程序链接到Firebase有什么不同? - How do I link a module (not project) in android studio to firebase? how is it different from linking app to firebase? 如何根据 firebase-realtime-database 中特定值的存在在 android studio 中显示数据列表? - How do I show a list of data in android studio based on the presence of a particular value in firebase-realtime-database? 如何从另一个线程或进程中提取数据(Android / Java) - How do I pull data from another thread or process (Android/Java) 如何让 Firebase 告诉我用户崩溃的确切代码行? - How do I get Firebase to tell me the exact line of code a user crashed at? 如何从Firebase正确提取数据? - How to properly pull my data from Firebase? 如何在Android中从数组中提取字符串? - How do I pull a string from an array in Android? 如何等待侦听器从 Android Studio 中的 Firebase 读取数据? - How to wait for the listener to read the data from Firebase in Android Studio? 如何从 Android Studio 中的 Firebase 中删除推送键数据 - How to delete push key data from Firebase in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM