2

I'm trying to implement a FirebaseRecyclerAdapter For the first time, but it just never gets called! I've checked the Firebase Guide Here but no good.

Have also checked similar threads that reported "Deleting Has FixedSize()" fixed it for them but that's not the case Here

Here's part of my database

and Here's my code for the adapter and model:

 FirebaseRecyclerAdapter<Complaint, ComplaintHolder> TestAdapter;
DatabaseReference mRef;
FirebaseUser mUser;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUser = FirebaseAuth.getInstance().getCurrentUser();
    mRef = FirebaseDatabase.getInstance().getReference("AccountsComplaintBasdNode").child(mUser.getUid());


    Query query = mRef;


    FirebaseRecyclerOptions<Complaint> mOptions = new FirebaseRecyclerOptions.Builder<Complaint>()
            .setQuery(query, Complaint.class)
            .build();


    if (TestAdapter == null) {

        TestAdapter = new FirebaseRecyclerAdapter<Complaint, ComplaintHolder>(mOptions) {
            @NonNull
            @Override
            public ComplaintHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View v = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, viewGroup, false);
                Toast.makeText(getActivity(), "Please get called", Toast.LENGTH_SHORT).show();
                return new ComplaintHolder(v);
            }


            @Override
            protected void onBindViewHolder(@NonNull ComplaintHolder holder, int position, @NonNull Complaint model) {

                holder.TitleTv.setText(model.getComplaintTitle());
                SimpleDateFormat smf = new SimpleDateFormat("yyyy/MM/dd");
                String dateString = smf.format(model.getDate());
                holder.DateTv.setText(dateString);

            }


///Model Object here

 public Complaint() {
}

private String ComplaintTitle;
private String details;
private Date mDate;
private String PhotoUrl;



public Complaint(String complaintTitle, String details, Date date, String photoUrl) {
    ComplaintTitle = complaintTitle;
    this.details = details;
    mDate = date;
    PhotoUrl = photoUrl;
}

public void setComplaintTitle(String complaintTitle) {
    ComplaintTitle = complaintTitle;
}

public void setDetails(String details) {
    this.details = details;
}

public void setDate(Date date) {
    this.mDate = date;
}

public void setPhotoUrl(String photoUrl) {
    PhotoUrl = photoUrl;
}

public String getComplaintTitle() {
    return ComplaintTitle;
}

public String getDetails() {
    return details;
}

public Date getDate() {
    return mDate;
}

public String getPhotoUrl() {
    return PhotoUrl;
}
gemy845
  • 84
  • 6

1 Answers1

0

The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.

So seems only you need is to call TestAdapter.startListening(); in onStart() method and don't forget to call TestAdapter.stopListening(); in onStop() as well but before calling stopListening() always check agains null like:if(TestAdapter != null)

For more information and example: https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md

Yupi
  • 4,402
  • 3
  • 18
  • 37