firebase循环视图未显示

时间:2017-09-23 22:44:45

标签: android firebase firebase-realtime-database firebaseui

我试图在回收查看器中读取一个列表,但它没有显示给我。

这个addfood.java - 它是我将在其中显示博客流程的类:

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);



    }
}

这是blog1.java这是:

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>

1 个答案:

答案 0 :(得分:1)

我有同样的问题,但后来我看到这个解决方案,事实证明适配器需要开始监听,它必须全局化 要解决此问题,首先要使适配器变量为全局变量:

private FirebaseRecyclerAdapter<UserInformation, EmployeeViewHolder> adapter;

然后你需要在onStart()和onStop()方法中添加以下代码行:

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

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

因为适配器使用侦听器来检查数据库更改,所以为了使其工作,您需要先开始侦听。当您关闭应用程序时,您需要停止收听。