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

标签 java android firebase firebase-realtime-database

因此,我进行了设置,希望从 Firebase 上的数据库获取数据,我可以正常获取 URL,但无法从中获取数据。当我尝试调试时,它会读取 query.addValueEventListener 但之后它会直接进入我的列表适配器。我不知道它没有获取数据。我的节点服务器上有一个类似的设置,运行得很好,但由于某种原因无法在 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);

database

最佳答案

它直接转到适配器,因为addValueEventListener()异步监听器,它在后台运行!

试试这个代码,它可能会对你有帮助!

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) {

        }
    });

关于java - 无法从 Android 上的 Firebase 数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49841933/

相关文章:

java - 如何将 Maven 构建的工件部署到 SourceForge 文件发布系统?

java - 将纪元时间戳转换为可读日期不起作用

安卓有什么安全问题吗?

java - 等待 Firestore 数据被检索

javascript - 如何更新html表中的firebase数据库数据?

java - 将 InputStream 对象转换为 String 的最佳方法

java - 为什么 Truffle DSL 添加 @Specialization 注解时会出错?

android - 需要获取捕获图像的当前方向

android - Zxing 相机在 Android 上的人像模式

node.js - 使用 Nodejs 获取 Firebase 云消息传递历史记录