Java – How do I implement the Firebase Recycler Adapter in newer versions of Android 3.1 and later?

How do I implement the Firebase Recycler Adapter in newer versions of Android 3.1 and later?… here is a solution to the problem.

How do I implement the Firebase Recycler Adapter in newer versions of Android 3.1 and later?

Basically, what I want to do is use the FirebaseRecyclerAdapter and populate the RecyclerView with my custom-designed CardView. The code for the new version has changed, therefore, I tried to implement it without success.
This is the code I wrote a year ago and it works well and populates my RecyclerView:

FirebaseRecyclerAdapter<DataClass,DataViewHolder> FBRA= new FirebaseRecyclerAdapter<DataClass, DataViewHolder>(
            DataClass,
            R.layout.myCardView,
            DataViewHolder.class,
            databaseReference
    ) {
        @Override
        protected void populateViewHolder(DataViewHolder viewHolder, DataClass model, int position) {
            viewHolder.setTitle(model.gettitle());
            viewHolder.setDate(model.getDate());
        }
   }; 
  myRecyclerView.setAdapter(FBRA);

Now we have to use something like this,
But the problem is that this code is not populating my recyclerView (what changes do I need to make here to populate my recyclerView with my cardView?). )

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

Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Official_Services");

FirebaseRecyclerOptions<ServiceClass> options = new FirebaseRecyclerOptions.Builder<ServiceClass>()
            .setQuery(query, ServiceClass.class)
            .build();

FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder> FBRA = new FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>(options) {

@NonNull
        @Override
        public ServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(HomeActivity.this).inflate(R.layout.service_card, parent, false);

return new ServiceViewHolder(view);

}

@Override
        protected void onBindViewHolder(@NonNull ServiceViewHolder holder, int position, @NonNull ServiceClass model) {

holder.setServiceName(model.getServiceName());

holder.setServiceCaption(model.getServiceCaption());

}

};

mServiceList.setAdapter(FBRA);

}

This is my ViewHolder class:

public static class ServiceViewHolder extends RecyclerView.ViewHolder {

public ServiceViewHolder(View itemView) {

super(itemView);

View mView = itemView;

}

public void setServiceName(String serviceName) {

TextView sName = itemView.findViewById(R.id.serviceName);

sName.setText(serviceName);

}

public void setServiceCaption(String serviceCaption) {

TextView sCaption = itemView.findViewById(R.id.serviceCap);

sCaption.setText(serviceCaption);
    }

}

Here are my getter and setter model classes:

public class ServiceClass {

private String serviceName;
private String serviceCode;
private String serviceCaption;
private String serviceIconUrl;

public ServiceClass() {
}

public ServiceClass(String serviceName, String serviceCode, String serviceCaption, String serviceIconUrl) {
    this.serviceName = serviceName;
    this.serviceCode = serviceCode;
    this.serviceCaption = serviceCaption;
    this.serviceIconUrl = serviceIconUrl;
}

public String getServiceName() {
    return serviceName;
}

public String getServiceCode() {
    return serviceCode;
}

public String getServiceCaption() {
    return serviceCaption;
}

public String getServiceIconUrl() {
    return serviceIconUrl;
}

public void setServiceName(String serviceName) {
    this.serviceName = serviceName;
}

public void setServiceCode(String serviceCode) {
    this.serviceCode = serviceCode;
}

public void setServiceCaption(String serviceCaption) {
    this.serviceCaption = serviceCaption;
}

public void setServiceIconUrl(String serviceIconUrl) {
    this.serviceIconUrl = serviceIconUrl;
}

@Override
public String toString() {
    return "ServiceClass{" +
            "serviceName='" + serviceName + '\'' +
            ", serviceCode='" + serviceCode + '\'' +
            ", serviceCaption='" + serviceCaption + '\'' +
            ", serviceIconUrl='" + serviceIconUrl + '\'' +
            '}';
}
}

What changes do I need to make now?

Here is my entire java file:

public class HomeActivity extends AppCompatActivity {

private RecyclerView mServiceList;

private FirebaseDatabase mDatabase;

private DatabaseReference myRef;

FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder> FBRA;

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

BottomNavigationViewEx bottomNavigationViewEx = findViewById(R.id.navViewBar);

bottomNavigationViewEx.enableAnimation(false);

bottomNavigationViewEx.enableShiftingMode(false);

bottomNavigationViewEx.setTextVisibility(false);

Calligrapher calligrapher = new Calligrapher(this);

calligrapher.setFont(this, "Helvetica.ttf", true);

mServiceList = findViewById(R.id.serviceRV);

mServiceList.setHasFixedSize(true);

mServiceList.setLayoutManager(new LinearLayoutManager(this));

mDatabase = FirebaseDatabase.getInstance();

myRef = mDatabase.getReference().child("Official_Services");

}

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

FBRA.startListening();

Query query = myRef;

FirebaseRecyclerOptions<ServiceClass> options = new FirebaseRecyclerOptions.Builder<ServiceClass>()
                .setQuery(query, ServiceClass.class)
                .build();

FBRA = new FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>(options) {

@NonNull
            @Override
            public ServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(HomeActivity.this).inflate(R.layout.service_card, parent, false);

return new ServiceViewHolder(view);

}

@Override
            protected void onBindViewHolder(@NonNull ServiceViewHolder holder, int position, @NonNull ServiceClass model) {

holder.setServiceName(model.getServiceName());

holder.setServiceCaption(model.getServiceCaption());

}

};

mServiceList.setAdapter(FBRA);

}

public static class ServiceViewHolder extends RecyclerView.ViewHolder {

public ServiceViewHolder(View itemView) {

super(itemView);

View mView = itemView;

}

public void setServiceName(String serviceName) {

TextView sName = itemView.findViewById(R.id.serviceName);

sName.setText(serviceName);

}

public void setServiceCaption(String serviceCaption) {

TextView sCaption = itemView.findViewById(R.id.serviceCap);

sCaption.setText(serviceCaption);
        }

}

}

Solution

To be able to display data from the Firebase live database, you need to start listening for changes, and for this you should add the following line of code method to onStart():

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

To stop listening for changes, you need to add the following line of code in onStop() like this:

@Override
protected void onStop() {
    super.onStop();
    if(FBRA != null) {
        FBRA.stopListening();
    }
}

See me in this The answer in post I explained in it why you should remove the listener.

Also don’t forget to make an FBRA global variable and remove FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder> declarations from objects.

Related Problems and Solutions