如何等待从数据库中提取数据?

时间:2018-02-15 22:23:58

标签: android firebase firebase-realtime-database

我正在尝试将Firebase数据库中的数据打印到android studio logcat中。 我能够将数据存储到列表中但我无法在logcat中打印列表。我认为这是因为在获取来自firebase的数据之前打印了数组。我不确定如何在获取来自firebase的数据后进行打印。

任何帮助将不胜感激,谢谢!

// Main activity

package com.imaginationcreators.bvnlibrary;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);

        Search search = new Search();

        search.setLocalDatabase();


        for(int i = 0; i < search.genre.size(); i++ ) {
            Log.d("String", search.genre.get(i));
        }


    }




}
package com.imaginationcreators.bvnlibrary;


import android.util.Log;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

public class Search {



    private ArrayList<Books> searchResults = new ArrayList<>();

    private int numberOfResults;
    private int childsAdded = 0;

    private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
    public ArrayList<String> genre = new ArrayList<>();
 public Search()
    {
        Log.d("String", "afterdatabasesearchclass");
    }

 public void setLocalDatabase()
    {
        Log.d("String", "notworking");

        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
                {
                    genre.add(dataSnapshot1.getKey());

                }

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d("pogo", databaseError.getDetails());
            }
        });



    }
}

1 个答案:

答案 0 :(得分:1)

Firebase API是异步的,这意味着它们会立即返回。运行以下代码时:

databaseReference.addChildEventListener(...)

addChildEventListener立即返回,查询结果将在稍后的某个时间传递给您的回调。只有在调用回调后才能访问快照。您的代码在结果准备好之前尝试访问它,因为setLocalDatabase()会在结果准备好之前立即返回。

Please read this blog更好地了解Firebase API异步的原因以及期望的内容。