Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseRecyclerAdapter can't retrieve data from Firebase Database

I'm trying to show Blog Posts on my MainActivity (Home page). But Homepage Nothing shows I mean Homepage is empty.

Firebase database structure:

enter image description here

MainActivity:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private RecyclerView mBlogList;
    private DatabaseReference mDataRef;
    private FirebaseRecyclerAdapter<BlogInfo, ViewHolder> adapter;

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

        Log.d(TAG, "onCreate: started!");
        mDataRef = FirebaseDatabase.getInstance().getReference().child("BlogApp").child("Posts");
        mBlogList = findViewById(R.id.blogListId);
        mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerOptions<BlogInfo> options = new FirebaseRecyclerOptions.Builder<BlogInfo>()
                .setQuery(mDataRef, BlogInfo.class).build();

        adapter = new FirebaseRecyclerAdapter<BlogInfo, ViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull BlogInfo model) {

                String imgUrl = model.getImage();
                Picasso.get().load(imgUrl).into(holder.post_image);
                holder.post_title.setText(model.getTitle());
                holder.post_desc.setText(model.getDescription());

            }

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                Log.d(TAG, "onCreateViewHolder: initialized!");
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_blog_item, parent, false);
                ViewHolder holder = new ViewHolder(view);
                return holder;
            }
        };
        mBlogList.setAdapter(adapter);

    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
        Log.d(TAG, "onStart: init");
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
        Log.d(TAG, "onStop: stop Blog list");
    }

    // ViewHolder Class
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private static final String TAG = "ViewHolder";
        TextView post_title;
        TextView post_desc;
        ImageView post_image;

        ViewHolder(View itemView) {
            super(itemView);
            Log.d(TAG, "ViewHolder: Initialized!");
            post_title = itemView.findViewById(R.id.postTitleId);
            post_desc = itemView.findViewById(R.id.postDescId);
            post_image = itemView.findViewById(R.id.postImageId);
        }
    }
}

BlogInfo.class:

public class BlogInfo {
    private String description;
    private String image;
    private String title;

    public BlogInfo() {}

    public BlogInfo(String title, String description, String image) {
        this.title = title;
        this.description = description;
        this.image = image;
    }

    public String getTitle() {return title;}

    public void setTitle(String title) {this.title = title;}

    public String getDescription() {return description;}

    public void setDescription(String description) {this.description = description;}

    public String getImage() {return image;}

    public void setImage(String image) {this.image = image;}
}

This is my first time I'm trying to use FirebaseRecyclerAdapter to retrieve data from Firebase Database. But I don't know where the problem is. I tried to debug also but the compiler won't enter into onCreateViewHolder or onBindViewHolder function. But I'm totally following FirebaseUI for Realtime Database reference.

like image 816
Sheikh Hasib Avatar asked Jul 30 '26 11:07

Sheikh Hasib


1 Answers

To solve this, you need to remove the following line of code:

mBlogList.setHasFixedSize(true);

This is needed only when you want your RecyclerView to have a fixed size. In your case is not necessary.

like image 198
Alex Mamo Avatar answered Aug 01 '26 23:08

Alex Mamo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!