Android, on finish loading data from firebase

故事扮演 提交于 2020-01-14 06:34:15

问题


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?

In onCreate method I am showing my ProgressBar but I don't know when should I hide it.

Can you guys give me some ideas? Thanks


回答1:


Assuming you are using a FirestoreRecyclerAdapter, to solve this, create a new object of ProgressBar and start showing it in the onCreate() method, or if you want add it directly in your .XML file and finally in your adapter class, override the following method:

@Override
public void onDataChanged() {
    if (progressBar != null) {
        progressBar.setVisibility(View.GONE);
    }
}



回答2:


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


来源:https://stackoverflow.com/questions/52004907/android-on-finish-loading-data-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!