firebaserecycleradapter() cannot be applied to FirebaseRecyclerAdapter

25,827

Solution 1

The latest version (3.x) of FirebaseUI implements a different method of initializing a FirebaseRecyclerAdapter than previous versions. From the using the FirebaseRecyclerAdapter documentation:

First, configure the adapter by building FirebaseRecyclerOptions. In this case we will continue with our chat example:

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

Next create the FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass for displaying each item.

So, for your example, you'll need to do something similar to:

FirebaseRecyclerOptions<image_details> options =
        new FirebaseRecyclerOptions.Builder<image_details>()
                .setQuery(myRef, image_details.class)
                .build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(options) {
    @Override
    public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.individual_row, parent, false);

        return new BlogViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
        // Bind the image_details object to the BlogViewHolder
        // ...
    }
};

Finally, the FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call adapter.startListening() and adapter.stopListening() to stop the listener. It's recommended to call these in onStart() and onStop() respectively. Further details on this are available in the FirebaseRecyclerAdapter lifecycle documentation.

Solution 2

You missed a method to start the activity. Add this in onStart() method:

firebaseRecyclerAdapter.startListening();

Solution 3

Hello your error is due to a missing argument in these () that are found beside this

<image_details, BlogViewHolder>

so you missed one more argument of your BlogViewHolder class so I will add the piece of code and refer your missing argument by a comment in this :

FirebaseRecyclerAdapter<image_details, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(
            image_details.class,
            R.layout.individual_row,
            BlogViewHolder.class //see this is missing so you should add it       
            myRef) {
Share:
25,827
Sourav Singh
Author by

Sourav Singh

Updated on March 24, 2020

Comments

  • Sourav Singh
    Sourav Singh about 4 years

    Can someone please tell what's the error in that specific line.

    Here is the image

    Here is the error message showing.

    Error Message

    package com.example.souravkumar.sqaurewallpapers;
    
    import android.os.Bundle;
    import android.os.PersistableBundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.firebase.ui.database.FirebaseRecyclerAdapter;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.squareup.picasso.Picasso;
    
    /**
     * Created by Sourav Kumar on 11/3/2017.
     */
    
    public class popular extends AppCompatActivity {
    
        private RecyclerView recyclerView;
        private DatabaseReference myRef;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
            super.onCreate(savedInstanceState, persistentState);
            recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            myRef = FirebaseDatabase.getInstance().getReference();
            FirebaseRecyclerAdapter<image_details, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(
                    image_details.class,
                    R.layout.individual_row,
    
                    myRef) {
                @Override
                protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
                    holder.setDate(model.getDate());
                    holder.setUrl(model.getUrl());
                }
    
                @Override
                public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    return null;
                }
            };
    
            recyclerView.setAdapter(adapter);
    
        }
    
        public static  class BlogViewHolder extends RecyclerView.ViewHolder {
            TextView textView;
            ImageView imageView;
            public BlogViewHolder(View itemView) {
                super(itemView);
                textView = (TextView)itemView.findViewById(R.id.date);
                imageView = (ImageView)itemView.findViewById(R.id.imageView);
            }
    
            public void setDate(Long date) {
                textView.setText(date.toString());
            }
    
            public void setUrl(String url) {
                Picasso.with(itemView.getContext())
                        .load(url)
                        .resize(50, 50)
                        .centerCrop()
                        .into(imageView);
            }
        }
    }
    

    Link to the whole code

    • Frank van Puffelen
      Frank van Puffelen over 6 years
      Please don't show pictures of code (or of other textual content). Instead edit your question to show the actual code, and include the text of the error message you get. See Creating an MCVE.
    • Sourav Singh
      Sourav Singh over 6 years
      ok..but can you please help me here? need help
    • Grimthorr
      Grimthorr over 6 years
      What version of FirebaseUI are you using and is image_details a model class? Please share your code as text and the entire error message you receive.
    • Sourav Singh
      Sourav Singh over 6 years
      firebase Ui: compile 'com.firebaseui:firebase-ui-database:3.1.0'
    • Sourav Singh
      Sourav Singh over 6 years
      and yes image_details is model class
    • Sourav Singh
      Sourav Singh over 6 years
      @Grimthorr I have updated the error message showing, look into the picture
    • Sourav Singh
      Sourav Singh over 6 years
      @Grimthorr can you please look into my project and tell me where i'm going wrong. I have been struggling from almost a week to retreive data from firebase database to show it on fragment of a tabbed-activity.
  • Sourav Singh
    Sourav Singh over 6 years
    Bind the image_details object how?
  • Grimthorr
    Grimthorr over 6 years
    Like you would when creating a normal RecyclerView, see creating lists and cards Android documentation. So something like: holder.textView.setText(model.getText()); or whatever method your image_details model has for getting the text that you want to display on the row's TextView.
  • Sourav Singh
    Sourav Singh over 6 years
    Sir, I am still not able to get output on my fragment screen. can you please look into my project and tell me what's wrong?