我正在尝试阅读列表,形成回收站的消防基地,但它没有向我显示。
这个addfood.java
-这是我将在其中显示博客流的类:
软件包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>
我遇到了同样的问题要解决这个问题,首先使您的适配器变量全局:
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();
}
}
由于适配器使用侦听器检查数据库更改,因此为了使其正常工作,您需要先开始侦听。当您关闭应用程序时,您需要停止聆听。