Android, on finish loading data from firebase

前端 未结 2 1911
予麋鹿
予麋鹿 2021-01-17 06:27

How to show the Progressbar on start of the activity that contains recycler view which should be hidden once the recycler view loads the data from firebase database?

2条回答
  •  时光说笑
    2021-01-17 07:07

    You should give your code. But i will try to explain with my code. (My code use LovelyProgressDialog as ProgressBar, because i like the UI. But you can change with your prefered ProgressBar)

    First, you can show the progress bar in onCreate method (or onCreateView if you use Fragment instead of Activity).

    @Override
    public View onCreate(Bundle savedInstanceState) {
            //Enter your code here
            dialogGetAllData.setCancelable(false)
                    .setIcon(R.drawable.ic_add_friend)
                    .setTitle("Get all friend....")
                    .setTopColorRes(R.color.colorPrimary)
                    .show();
            getListData();
    }
    

    Then after you get all of the data from firebase (it means the dataSnapshot already null), you dismiss your progress bar

        private void getListData() {
        FirebaseDatabase.getInstance().getReference().child("friend/" + StaticConfig.UID).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.getValue() != null) {
                    HashMap mapRecord = (HashMap) dataSnapshot.getValue();
                    Iterator listKey = mapRecord.keySet().iterator();
                    while (listKey.hasNext()) {
                        String key = listKey.next().toString();
                        listFriendID.add(mapRecord.get(key).toString());
                    }
                } else {
                    dialogGetAllData.dismiss();
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
    

提交回复
热议问题