0

I want to show a Progressbar when my data is being retrived from firebasedatabase into recyclerview, and hide it when its done. I searched online but no soln. works

here is my mainActivity

public class MemesSchool extends AppCompatActivity {

  RecyclerView recyclerView;
  MemeAdapter memeAdapter;
  ProgressBar progressBar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    progressBar = (ProgressBar) findViewById(R.id.pbmeme);

    setContentView(R.layout.activity_memes_school);
    getWindow().setStatusBarColor(getResources().getColor(R.color.buttonclr));
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.buttonclr)));
    getSupportActionBar().setTitle("School Memes");

    recyclerView = findViewById(R.id.rvm);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    FirebaseRecyclerOptions < mememodel > options =
      new FirebaseRecyclerOptions.Builder < mememodel > ()
      .setQuery(FirebaseDatabase.getInstance().getReference().child("Memedata"), mememodel.class)
      .build();

    memeAdapter = new MemeAdapter(options);
    recyclerView.setAdapter(memeAdapter);

  }

  @Override
  protected void onStart() {
    super.onStart();
    memeAdapter.startListening();
  }

  @Override
  protected void onStop() {
    super.onStop();
    memeAdapter.stopListening();
  }
}

here is my MainActivity.XML

<?xml version="1.0" encoding="utf-8"?>
   <androidx.constraintlayout.widget.ConstraintLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MemesSchool">

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/rvm"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foregroundGravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:id="@+id/pbmeme"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
   </androidx.constraintlayout.widget.ConstraintLayout>

my Adapterclass

public class MemeAdapter extends FirebaseRecyclerAdapter < mememodel, MemeAdapter.myViewHolder > {

  public MemeAdapter(@NonNull FirebaseRecyclerOptions < mememodel > options) {
    super(options);
  }

  @Override
  protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull mememodel model) {

    holder.username.setText(model.getNameuser());
    holder.subuser.setText(model.getSubname());
    holder.title.setText(model.getTitle());
    Glide.with(holder.imgmeme.getContext()).load(model.getImagesurl())
      .placeholder(R.drawable.logoinhanced)
      .error(R.drawable.logoinhanced).into(holder.imgmeme);

  }

  @NonNull
  @Override
  public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view =
      LayoutInflater.from(parent.getContext()).inflate(R.layout.memes_item, parent, false);

    return new myViewHolder(view);
  }

  @Override
  public void onDataChanged() {

  }

  @Override
  public void onError(@NonNull DatabaseError error) {
    super.onError(error);

  }

  class myViewHolder extends RecyclerView.ViewHolder {

    ImageView imgmeme;
    TextView username, subuser, title;

    public myViewHolder(@NonNull View itemView) {
      super(itemView);

      imgmeme = itemView.findViewById(R.id.meme);
      username = itemView.findViewById(R.id.nameuser);
      subuser = itemView.findViewById(R.id.subname);
      title = itemView.findViewById(R.id.titlememe);
    }
  }
}

There Is an Mothod called ONDATACHANGE() when should be in adapter class and i dont know how to control my adapter class from there ;

what should happen :- a Progressbar should be visible when i open the Activity and it should hide when the data is loaded in the RecyclerView !

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gr8 BEAST
  • 13
  • 5
  • 1
    What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Aug 02 '22 at 11:17
  • I dont have an error yet, I want to add a progressbar when the data is being loaded but i m not able to getany soln – Gr8 BEAST Aug 04 '22 at 12:55
  • @AlexMamo I have seen your answer on a seperate question.. of same type your answer was Override public void onDataChanged() { if (progressBar != null) { progressBar.setVisibility(View.GONE); } } i havse Some question, as we cannot control our progressbar from aadapter class so how can we do that.....And if possible then please help me by replaying or lets talk On discord please ! – Gr8 BEAST Aug 04 '22 at 14:44
  • Yes, that's true/ I answered a similar question a few years ago. So what is the issue with that? What is the issue with Frank's solution? – Alex Mamo Aug 05 '22 at 07:51
  • His soln is not working... It gives me an error as I mentioned in his comments, the solution that u gave like a few years ago, I can't understand that as we can't control the progress bar from the adapter... If we use franks soln then we can control but it gives me an error.. Please consider helping – Gr8 BEAST Aug 05 '22 at 12:09
  • Without knowing the error, we cannot be much of a help. – Alex Mamo Aug 05 '22 at 12:15
  • I figured out the easiest soln !... i just elevated the recyclerview above the progressbar and when even the data is loading hence the progressbar is visible, and one the data is retrieved then the progressbar is cover by the item in the rv .. – Gr8 BEAST Aug 07 '22 at 11:08
  • If it solves the problem, then it's a good solution. – Alex Mamo Aug 07 '22 at 11:55

1 Answers1

0

One way to hide the progress bar when the data is loaded is to pass it to your adapter in its constructor, and then hide it in onDataChanged:

public class MemeAdapter extends FirebaseRecyclerAdapter < mememodel, MemeAdapter.myViewHolder > {
  View progressBar;

  public MemeAdapter(@NonNull FirebaseRecyclerOptions <mememodel> options, View progressBar) {
    super(options);
    this.progressBar = progressBar;
  }

  ...

  @Override
  public void onDataChanged() {
    progressBar.setVisibility(View.GONE)
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • It doesn't work for me ! given an error - Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference at com.sjsbhints.SjsbHints.MemeAdapter.onDataChanged(MemeAdapter.java:68) – Gr8 BEAST Aug 04 '22 at 12:35
  • It sounds like your `progressBar` is not initialized. Did you search for that error message? – Frank van Puffelen Aug 04 '22 at 13:36
  • I dont think so, as I tried to hide to progressbar by typing "progress.setVisibility(View.GONE)" code in the MainActivity Oncreate and it works fine !...it just dosent work for ondatachange .... please help me solve this issue – Gr8 BEAST Aug 04 '22 at 13:52
  • The error message is pretty explicit about the fact that you're calling `progress.setVisibility` when `progress` is not initialized. Have a look here to learn how to fix *that* problem: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Aug 04 '22 at 15:12