K
K
kirvel2020-10-08 21:49:03
Java
kirvel, 2020-10-08 21:49:03

ProgressBar (Android) not updating?

Here is the code that has the following functionality: ArrayList is populated with objects from the Firebase database. I need the ProgressBar to update when a new object is added, but it doesn't. Why?

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("films");
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for(DataSnapshot dataSnapshot:snapshot.getChildren()) {
                movies.add(dataSnapshot.getValue(Movie.class));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.progressBar.setProgress((movies.size()/1962)*100);
                    }
                });
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });


Here is the ProgressBar itself:
<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/progressBar"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_marginStart="250dp"
    android:progress="20"
    android:max="100"
    android:indeterminate="false"
    android:layout_centerInParent="true"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-10-08
@zagayevskiy

First, because all your update calls happen in a row and all of them will be executed on the next frame, that is, only the last update will be effectively shown. Elements are added to the list almost instantly, that is, there will be no UI update there.
Second, I think you're counting progress wrong: (movies.size()/1962)*100. I don’t know what the magic number is, but I think that it always turns out 0. This is an integer division.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question