2017-05-29 71 views
0

我在我的應用中使用Firebase實時數據庫。我有listView和適配器,適配器的每個視圖都會從Firebase數據庫加載一個項目。但是加載所有數據需要幾秒鐘(每個項目都有圖像和名稱)。所以我想展示進度條或者向用戶顯示的東西,但我不知道該把它放在哪裏。加載Firebase數據庫時顯示進度條

我想過使用asynctask什麼的,但問題是我不知道數據在哪裏加載。我將子事件監聽器附加到Firebase數據庫引用對象,因此每當添加新的子代時,適配器都會更新,但我不知道如何知道所有子代都已加載完畢。

希望你能理解我並幫助我。

回答

1

看到這是設置一些數據的功能: -

 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()); 
       } 

      }); 
     }