Search code examples
javaandroidfirebasefirebase-realtime-databasefirebaseui

FirebaseRecyclerAdapter unable to populate result


I want to display EmployeeDetails by using FirebaseRecyclerAdapter in RecyclerView and by not using FirebaseListAdapter. But the activity is displaying nothing

Firebase:

 "EmployeeDetails" : {
    "3dTxdRlBVUZ0jzH4U5MR4TOKZnD2" : {
      "address" : "na",
      "name" : "na",
      "phoneNum" : "na",
      "type" : "Employee"
    },
    "wEz6XRMQcAU9cQJLR82uVwVMrC83" : {
      "address" : "na",
      "name" : "na",
      "phoneNum" : "na",
      "type" : "Admin"
    }
  }

ViewEmployeeDetails.java

public class ViewEmployeeDetails extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    DatabaseReference employee;

    FirebaseRecyclerAdapter<UserInformation,EmployeeViewHolder> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_employee_details);

        recyclerView=(RecyclerView)findViewById(R.id.listEmployee);
        recyclerView.setHasFixedSize(true);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        loadEmployee();
    }

    private void loadEmployee() {
        employee= 
     FirebaseDatabase.getInstance().getReference("EmployeeDetails");
        adapter=new FirebaseRecyclerAdapter<UserInformation, EmployeeViewHolder>
        (
                UserInformation.class,
                R.layout.employee_list_activity,
                EmployeeViewHolder.class,
                employee
        ) {
            @Override
            protected void populateViewHolder(EmployeeViewHolder viewHolder, 
     UserInformation model, int position) {
                viewHolder.txtEmployeeName.setText(model.getName());
                viewHolder.txtEmployeeType.setText(model.getType());
                viewHolder.txtEmployeeAddress.setText(model.getAddress());
                viewHolder.txtEmployeePhone.setText(model.getPhoneNum());
            }
        };
        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);
    }
}

EmployeeViewHolder.java

package com.example.intel.employeesite.Other.ViewHolders;

   import android.support.v7.widget.RecyclerView;
   import android.view.View;
   import android.widget.TextView;
   import com.example.intel.employeesite.R;


   public class EmployeeViewHolder extends RecyclerView.ViewHolder {
    public TextView 
    txtEmployeeName,txtEmployeeAddress,txtEmployeePhone,txtEmployeeType;
    public EmployeeViewHolder(View itemView) {
        super(itemView);

        txtEmployeeName=(TextView) itemView.findViewById(R.id.txtEmployeeName);
        txtEmployeeAddress=(TextView) 
    itemView.findViewById(R.id.txtEmployeeAddress);
        txtEmployeePhone=(TextView) 
    itemView.findViewById(R.id.txtEmployeePhone);
        txtEmployeeType=(TextView) itemView.findViewById(R.id.txtEmployeeType);
    }
}

activity_view_employee_details.xml

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context="com.example.intel.employeesite.Admin.ViewEmployeeDetails">

   <android.support.v7.widget.RecyclerView
        android:id="@+id/listEmployee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

employee_list_activity.xml

 <CardView
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="8dp"
       app:cardElevation="10dp"
       android:id="@+id/empinfo"
       android:layout_margin="5dp">

        <LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

        <TextView
            android:id="@+id/txtEmployeeName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Employee Name"
            android:textSize="20sp"
            android:background="#4f0e0d0e"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/txtEmployeeAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Employee Address"
            android:textSize="20sp"
            android:background="#4f0e0d0e"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/txtEmployeePhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Employee Phone"
            android:textSize="20sp"
            android:background="#4f0e0d0e"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/txtEmployeeType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Employee Type"
            android:textSize="20sp"
            android:background="#4f0e0d0e"
            android:textColor="#fff"/>

       </LinearLayout>
    </CardView>

I try to solve it but nothing happens. Someone helps me out.


Solution

  • To solve this, first make your adapter variable global:

    private FirebaseRecyclerAdapter<UserInformation, EmployeeViewHolder> adapter;
    

    Then you need to add the following lines of code in your onStart() and onStop() methods:

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

    Because the adapter uses a listener to check for database changes, in order to make it work, we need to start listening first. And when we close the application, we need to stop listening.

    I have explained in one of my videos, how to achieve this, step by step.