如何在进程运行时显示进度条?

时间:2018-04-27 03:03:51

标签: android android-progressbar

所以在OnCreate我运行一个用populateDataLogin调用的指定函数。现在我尝试在populateDataLogin运行时显示progressBar,并在进程完成时关闭它。这是我做的事情

progressBar.setVisibility(View.VISIBLE);
    progressBar.post( new Runnable() {
        public void run() {
            populateDataLogin();
            inputEmail.setEnabled(true);
            inputFullname.setEnabled(true);
            inputPassword.setEnabled(true);
            inputTelp.setEnabled(true);
            inputAlamat.setEnabled(true);
            progressBar.setVisibility(View.INVISIBLE);
        }
    });
好吧,我认为因为populateDataLogin()太快了。

这是我的populateDataLogin()

protected void populateDataLogin() {
        super.onStart();

        if (acct != null) {
            RequestOptions requestOptions = new RequestOptions();
            requestOptions.override(300,300);

            Glide   .with(MainActivity.this)
                    .load(acct.getPhotoUrl())
                    .apply(requestOptions)
                    .into(imagess);

            inputPassword.setVisibility(View.GONE);
            //CEK JIKA SUDAH ADA DI FIREBASE

            mDatabase = FirebaseDatabase.getInstance().getReference();
            BiodataTable = mDatabase.child("Biodata");
            Query queryToGetData = BiodataTable
                    .orderByChild("Email").equalTo(acct.getEmail());

            queryToGetData.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.getValue() != null) {
                        for (DataSnapshot bioSnap : dataSnapshot.getChildren()) {
                            personName = bioSnap.child("Fullname").getValue().toString();
                            personAlamat = bioSnap.child("Alamat").getValue().toString();
                            personEmail = bioSnap.child("Email").getValue().toString();
                            personTelp = bioSnap.child("NoTelp").getValue().toString();

                            inputEmail.setText(personEmail);
                            inputFullname.setText(personName);
                            inputAlamat.setText(personAlamat);
                            inputTelp.setText(personTelp);
                        }
                    }else{
                        personName = acct.getDisplayName();
                        personEmail = acct.getEmail();

                        inputEmail.setText(personEmail);
                        inputFullname.setText(personName);

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }

            });

            return;
        }
        Mauth = FirebaseAuth.getInstance();

        if(Mauth != null){
            mDatabase = FirebaseDatabase.getInstance().getReference();
            BiodataTable = mDatabase.child("Biodata");
            Query queryToGetData = BiodataTable
                    .orderByChild("Email").equalTo(Mauth.getCurrentUser().getEmail());

            queryToGetData.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    for (DataSnapshot bioSnap: dataSnapshot.getChildren()) {
                        personName = bioSnap.child("Fullname").getValue().toString();
                        personAlamat = bioSnap.child("Alamat").getValue().toString();
                        personEmail = bioSnap.child("Email").getValue().toString();
                        personTelp = bioSnap.child("NoTelp").getValue().toString();

                        inputEmail.setText(personEmail);
                        inputFullname.setText(personName);
                        inputAlamat.setText(personAlamat);
                        inputTelp.setText(personTelp);
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    }

那么为什么我要先显示progressBar?因为当用户连接速度慢时,我有点害怕。那我怎么能实现呢?

1 个答案:

答案 0 :(得分:0)

我希望这对你有用。

progressBar.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {
       populateDataLogin();
       inputEmail.setEnabled(true);
       inputFullname.setEnabled(true);
       inputPassword.setEnabled(true);
       inputTelp.setEnabled(true);
       inputAlamat.setEnabled(true);
       progressBar.setVisibility(View.INVISIBLE);
       }
   },2000);
}