0

I want to develop an app, Where it takes the string and convert it on word array which I already did. But the problem is when I use for loop and Firebase query to show Image from firebase storage one after one by thread.sleep(5000);.It's doesn't work. It's alway's show only last word image and also last word.

Here is my code:

                String str=matches.get(0).toLowerCase();

                final String[] word=str.split(" ");

                //speexhTxt.setText(word.length);

                // speexhTxt.setText(String.valueOf(one));

                for (i=0;i<word.length;i++) {
                    progressBar.setVisibility(View.VISIBLE);

                    imageHide.setVisibility(View.GONE);
                    signImage.setVisibility(View.VISIBLE);

                    firebaseFirestore= FirebaseFirestore.getInstance();
                    collectionReference=firebaseFirestore.collection("dumbsign");


                    Query query=collectionReference.whereEqualTo("word",word[i]);


                    final int finalI = i;
                    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {

                            if (task.isSuccessful()){

                                for (QueryDocumentSnapshot doc : task.getResult()){


                                    Glide.with(getApplicationContext()).
                                            load(doc.getString("image_url"))
                                            .into(signImage);
                                    speexhTxt.setText(word[finalI]);

                                    progressBar.setVisibility(View.GONE);

                                }
                            };

                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }


                    });




                }
bijoy
  • 1
  • 1

1 Answers1

0

You're putting your thread.sleep() after the loop is done, so basically what your code is doing is loading all the images and then sleeping the thread for 5000 milliseconds, you should put it inside the for loop, here for example:

for (QueryDocumentSnapshot doc : task.getResult()){


          Glide.with(getApplicationContext()).
          load(doc.getString("image_url")).into(signImage);
          speexhTxt.setText(word[finalI]);
          progressBar.setVisibility(View.GONE);
          try {
             Thread.sleep(5000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }

}

Note, i do NOT recommend using Thread.sleep() on your main Thread read this: How to call a method after a delay in Android

ivan
  • 1,177
  • 8
  • 23