简体   繁体   English

无法从Android上的Firebase数据库获取数据

[英]Can't get data from Firebase Database on Android

So I have a set up where I want to get data from the Database on Firebase, I can get the URL back fine but I can't get data from it. 因此,我进行了设置,希望从Firebase上的数据库中获取数据,我可以使URL恢复正常,但无法从中获取数据。 When I try to debug it reads in query.addValueEventListener but after that it just goes straight to my list adapter. 当我尝试调试时,它读入query.addValueEventListener但之后它直接进入我的列表适配器。 I don't know it's not getting the data. 我不知道没有得到数据。 I have a similar set up on my node server which works perfectly fine, but won't work on Android for some reason 我在节点服务器上进行了类似的设置,效果很好,但是由于某种原因无法在Android上运行

 final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
Query query = database.child("exercises");
Log.i("Database query", database.child("exercises").toString());
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot exerciseSnapshot: dataSnapshot.getChildren())
        {
            List<ExerciseList> exercises = new ArrayList<ExerciseList>();
            ExerciseList exerciseList = exerciseSnapshot.getValue(ExerciseList.class);
            Log.i("description/name", exerciseList.getDescription() + " " + exerciseList.getName());
            exercises.add(exerciseList);
            adapter = new ExerciseLoadListAdapter(mActivity, exercises);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
mListView.setAdapter(adapter);

数据库

its go to adapter directly because addValueEventListener() is Asynchronous listeners , its run in background ! 它直接进入adapter ,因为addValueEventListener()Asynchronous listeners ,它在后台运行!

Try this code , it may be help you ! 试试这个代码,可能对您有帮助!

final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
    //Query query = database.child("exercises");
    //Log.i("Database query", database.child("exercises").toString());
    database.child("exercises").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot exerciseSnapshot: dataSnapshot.getChildren())
            {
                List<ExerciseList> exercises = new ArrayList<ExerciseList>();
                ExerciseList exerciseList = exerciseSnapshot.getValue(ExerciseList.class);
                Log.i("description/name", exerciseList.getDescription() + " " + exerciseList.getName());
                exercises.add(exerciseList);

            }
               adapter = new ExerciseLoadListAdapter(mActivity, exercises);
               mListView.setAdapter(adapter)
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

The Firebase do NOT get the data in the debug mode in real time. Firebase 不会在调试模式下实时获取数据。 If you need some object from your firebase database you will need to get the object before. 如果您需要Firebase数据库中的某些对象,则需要先获取该对象。 I face this problem a several times, thats why in some cases I use SQLite to store data from Firebase before i take any action. 我几次面对这个问题,这就是为什么在某些情况下我使用SQLite在执行任何操作之前从Firebase存储数据的原因。 But, in a fragment you can not do that, you need the data in realtime. 但是,在一个片段中,您无法做到这一点,需要实时数据。 So you need to store the listener in a variable and use it after. 因此,您需要将侦听器存储在变量中,然后使用它。 Here in my Github page I have a fragment working fine. 在我的Github页面上,我有一个片段工作正常。 ContatsFragment.java []'s ContatsFragment.java []的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM