Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

firebase recycle view is not showing


I'm trying to read a list form a fire base in a recycle viewer, but it's not showing it to me.

this addfood.java - it's the class which i will show the blog flow in it:

package com.example.median1.sora;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.MenuView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.client.Firebase;
import com.firebase.client.core.Context;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

public class addfood extends AppCompatActivity {

    private RecyclerView mBlogList;
    private DatabaseReference mDatabase;


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


        mDatabase=FirebaseDatabase.getInstance().getReference("Blog");
      mBlogList=(RecyclerView)findViewById(R.id.blog_list);
      mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);




    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Blog1,BlogViewHolder> firebaseRecyclerAdapter1=new FirebaseRecyclerAdapter<Blog1, BlogViewHolder>(

                Blog1.class,
                R.layout.blog_row,
                BlogViewHolder.class,
                mDatabase
        ) {
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog1 model, int position) {
          viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
            }
        };

        mBlogList.setAdapter(firebaseRecyclerAdapter1);
    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder{
           View mView;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
        }
        public void setTitle(String title){

            TextView post_title=(TextView)mView.findViewById(R.id.post_title1);
            post_title.setText(title);

        }
        public void setDesc(String desc){


            TextView post_desc=(TextView)mView.findViewById(R.id.post_desc1);
            post_desc.setText(desc);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if(item.getItemId()==R.id.action_add){

          Intent u=new Intent(addfood.this,potedfood.class);
         startActivity(u);

        }
        return super.onOptionsItemSelected(item);



    }
}

this is blog1.java this is the:

package com.example.median1.sora;

/**
 * Created by median1 on 9/23/2017.
 */

public class Blog1 {

    private String title;
    private String desc;
    private String image;
    public Blog1(){



    }

    public Blog1(String title, String desc, String image) {
        this.title = title;
        this.desc = desc;
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getImage() {
        return image;
    }

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


}

addfood.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.median1.sora.addfood">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/blog_list"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

blog_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="match_parent">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/post_image1"
            android:src="@mipmap/add_btn"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"

            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/post_title1"
            android:hint="name"
            android:padding="10dp"
            android:textSize="16dp"
            android:textStyle="bold"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/post_desc1"
            android:hint="price"
            android:padding="10dp"
            android:textSize="16dp"
            android:textStyle="bold"/>


    </LinearLayout>

</LinearLayout>

Solution

  • i had this same issue but then i saw this solution it turns out the adapter needs to start listening and it has to be made global 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, you need to start listening first. And when you close the application, you need to stop listening.