简体   繁体   English

加载Firebase数据库时显示进度条

[英]Showing progress bar while loading Firebase database

I am using Firebase realtime database in my app. 我在我的应用程序中使用Firebase实时数据库。 I have listView and adapter that each view of the adapter load one item from the Firebase database. 我有listView和adapter,适配器的每个视图都从Firebase数据库加载一项。 But it takes few seconds to load all of the data (each item has image and name). 但是加载所有数据需要几秒钟(每个项目都有图像和名称)。 So I want to show progress bar or something to show to the user, but I don't know where to put it. 因此,我想显示进度条或要向用户显示的内容,但我不知道将其放在何处。

I thought about using asynctask or something, but the problem is that I don't really know where the data are loaded. 我考虑过使用asynctask之类的东西,但是问题是我真的不知道将数据加载到哪里。 I am attaching child event listener to Firebase database reference object, so any time a new child is added, the adapter will be updated, but I don't know how to know that all of the children had finished to load. 我将子事件侦听器附加到Firebase数据库引用对象,因此,每次添加新子项时,适配器都将更新,但是我不知道如何知道所有子项都已完成加载。

Hope you understand me and help me. 希望你能理解我并帮助我。

see this is a function to set some data:-- 看到这是设置一些数据的功能:-

     private void addUserChangeListener() {
    *** you can initialize your progress dialog here
                // User data change listener


      mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);


                    // Check for null
                    if (user == null) {
                        Log.e(TAG, "User data is null!");
                        return;
                    }

                    Log.e(TAG, "User data is changed!" + user.name + ", " + user.email);
    *** here you can check if the progress dialog is not null then close your progress dialog here

                    // Display newly updated name and email
                    txtDetails.setText(user.name + ", " + user.email);

                    // clear edit text
                    inputEmail.setText("");
                    inputName.setText("");

                    toggleButton();
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // Failed to read value
***also close your progress dialog here
                    Log.e(TAG, "Failed to read user", error.toException());
                }

            });
        }

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

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