1

I have a firebase recyclerview in which i am trying to populate some data from the firebase database. the data is loaded quite smoothly after 2 seconds of initial page-load. while the page is loaded initially, it usually displays 2 or 3 seconds of blank screen. i have tried to put a progress dialog during that period and have dismissed the progress dialog after the firebase recycler view is finished retrieving data from the firebase database. but the progress dialog never seems to appear at the first place. I am initializing my progress-dialog in the onCreate() method and dismissing the progress-dialog in the onResume() method right after the firebase recyclerview is done loading

I have tried to use progress-bar instead of progress dialog but the problem remain the same as i have metioned. i have also tried to use android Handler and timer method to provide it a default timing but that would a static method and not sufficient for a long term solution

    private ProgressDialog loadingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_task_list);


    // loading bar
    loadingBar = new ProgressDialog(this);
    loadingBar.setMessage("Please wait...");
    loadingBar.setCanceledOnTouchOutside(false);
    loadingBar.show();

     //Recyclerview and linear layout manager
    search_task_list_recycler = 
    findViewById(R.id.search_task_list_recycler);
    linearLayoutManager = new LinearLayoutManager(this);
    search_task_list_recycler.setHasFixedSize(true);
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    search_task_list_recycler.setLayoutManager(linearLayoutManager);


     }


 //   ====================================================================

     @Override
     protected void onResume() {
     super.onResume();

    // displaytask() contains the firebase recyclerview retrieval method 
    and recyclerview view 

    displayTasks();

    loadingBar.dismiss();


}


 //   ====================================================================

       private void displayTasks() {



       Query query = search_task_list;

       FirebaseRecyclerOptions<Search_task_details> options = new 
       FirebaseRecyclerOptions.Builder<Search_task_details>()
            .setQuery(query, Search_task_details.class).build();
       final FirebaseRecyclerAdapter<Search_task_details, ViewHolder> 
       adapter =
            new FirebaseRecyclerAdapter<Search_task_details, ViewHolder> 
        (options) {
                @Override
                protected void onBindViewHolder(@NonNull final ViewHolder 
      holder, int position, @NonNull final Search_task_details model) {



      UsersRef.child(model.getPoster_id()).addValueEventListener(new 
      ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot 
       dataSnapshot) {
                            if (dataSnapshot.exists()) {
                                if 
      (dataSnapshot.hasChild("profile_image")) {



Picasso.get().load(dataSnapshot.child("profile_image").getValue().toString())
                                             .placeholder(R.drawable.submit_profile_image)
                                            .into(holder.search_task_user_image);
                                }
                            }
                        }

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

                        }
                    });


                    holder.search_task_topic_title.setText(model.getTopic_title());
                    holder.search_task_due_date.setText(model.getDue_date());
                    holder.search_task_preferred_timing.setText(model.getPreferred_time());
                    holder.search_task_location.setText(model.getTask_location());
                    holder.search_task_job_status.setText(model.getJob_status());
                    holder.search_task_budget.setText(model.getSum_total() + " B.D");


                    String givenDateString = model.getCurrent_calendar_Date_and_Time();
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy, HH:mm", Locale.ENGLISH);
                    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Bahrain"));

                    long time_in_milliseconds = 0;

                    Date date = new Date();
                    try {
                        date = sdf.parse(givenDateString);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    time_in_milliseconds = date.getTime();


                    final CharSequence ch = DateUtils.getRelativeTimeSpanString(time_in_milliseconds
                            , System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
                    holder.search_task_time_ago.setText(ch);



                    holder.search_task_linear_layout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            postKey = getRef(holder.getAdapterPosition()).getKey();
                            Bundle bundle = new Bundle();
                            Intent goToTaskDetails = new Intent(Search_task_list.this, task_details.class);
                            bundle.putString("post_key", postKey);
                            bundle.putString("poster_id", model.getPoster_id());
                            goToTaskDetails.putExtras(bundle);
                            startActivity(goToTaskDetails);
                        }
                    });


                }

                @NonNull
                @Override
                public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.search_task_design_details, parent, false);
                    ViewHolder viewHolder = new ViewHolder(view);
                    return viewHolder;
                }
            };


     search_task_list_recycler.setAdapter(adapter);
     adapter.startListening();



}


//================================================================

    public static class ViewHolder extends RecyclerView.ViewHolder {
    public CircleImageView search_task_user_image;
    public AppCompatTextView search_task_topic_title;
    public AppCompatTextView search_task_due_date;
    public AppCompatTextView search_task_preferred_timing;
    public AppCompatTextView search_task_location;
    public AppCompatTextView search_task_time_ago;
    public AppCompatTextView search_task_job_status;
    public AppCompatTextView search_task_budget;
    public LinearLayout search_task_linear_layout;


    public ViewHolder(View itemView) {
        super(itemView);

        search_task_user_image = itemView.findViewById(R.id.search_task_user_image);
        search_task_topic_title = itemView.findViewById(R.id.search_task_topic_title);
        search_task_due_date = itemView.findViewById(R.id.search_task_due_date);
        search_task_preferred_timing = itemView.findViewById(R.id.search_task_preferred_timing);
        search_task_location = itemView.findViewById(R.id.search_task_location);
        search_task_time_ago = itemView.findViewById(R.id.search_task_time_ago);
        search_task_job_status = itemView.findViewById(R.id.search_task_job_status);
        search_task_budget = itemView.findViewById(R.id.search_task_budget);
        search_task_linear_layout = itemView.findViewById(R.id.search_task_linear_layout);


    }
}

Result Needed

The progress dialog should display as long as the firebase base recyclerview data is fetched from the firebase database. it can be 1 or 2 seconds or more depending upon other factors such as internetspeed or anything.

0

TRY THIS

private ProgressDialog loadingBar;

onCreate(Bundle savedInstanceState) {
     // loading bar
     loadingBar = new ProgressDialog(this);
     loadingBar.setMessage("Please wait...");
     loadingBar.setCanceledOnTouchOutside(false);
     loadingBar.show();

     displayTasks();
}

In Your displayTasks()

private void displayTasks() 
{
    search_task_list_recycler.setAdapter(adapter);
    adapter.startListening();

    //here add
    loadingBar.dismiss();
 }

Hope this will help you.

  • this did not work – saqib Jun 11 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.