Answer the question
In order to leave comments, you need to log in
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) {
}
});
<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
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 questionAsk a Question
731 491 924 answers to any question