我尝试使用RecyclerView从firebase检索数据 但遗憾的是它没有向我显示任何内容,也没有任何错误。 我不知道问题出在哪里。
这是我的第一堂课
package com.example.median1.helper;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceIdReceiver;
import org.w3c.dom.Text;
public class recyclerview extends AppCompatActivity {
private RecyclerView mbloglist;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mDatabase= FirebaseDatabase.getInstance().getReference().child("Request");
mbloglist=(RecyclerView)findViewById(R.id.blog_List);
mbloglist.setHasFixedSize(true);
mbloglist.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<blog,blogviewholder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<blog, blogviewholder>(
blog.class,R.layout.blog_row,blogviewholder.class,mDatabase
) {
@Override
protected void populateViewHolder(blogviewholder viewHolder, blog model, int position) {
viewHolder.settitle(model.getPosttitle());
viewHolder.setemail(model.getPostemail());
viewHolder.settmain(model.getPostmain());
}
};
}
public static class blogviewholder extends RecyclerView.ViewHolder {
View mView;
public blogviewholder(View itemView) {
super(itemView);
mView=itemView;
}
public void setemail(String postemail){
TextView post_email=(TextView)mView.findViewById(R.id.blogemail);
post_email.setText(postemail);
}
public void settitle(String posttitle){
TextView post_title=(TextView)mView.findViewById(R.id.blogtitle);
post_title.setText(posttitle);
}
public void settmain(String postmain){
TextView post_main=(TextView)mView.findViewById(R.id.blogmain);
post_main.setText(postmain);
}
}
}
这是它的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.helper.recyclerview">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/blog_List"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
这是我称之为博客的课程,它假定要完成电子邮件标题和主要
package com.example.median1.helper;
/**
* Created by median1 on 1/10/2018.
*/
public class blog
{
private String postemail;
private String postmain;
private String posttitle;
public blog(){
}
public blog(String postemail, String postmain, String posttitle) {
this.postemail = postemail;
this.postmain = postmain;
this.posttitle = posttitle;
}
public String getPostemail() {
return postemail;
}
public void setPostemail(String postemail) {
this.postemail = postemail;
}
public String getPostmain() {
return postmain;
}
public void setPostmain(String postmain) {
this.postmain = postmain;
}
public String getPosttitle() {
return posttitle;
}
public void setPosttitle(String posttitle) {
this.posttitle = posttitle;
}
}
这是我创建的用于在第一个类
中显示firebase内容的xml<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blogemail"
android:text="email"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blogtitle"
android:text="title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/blogmain"
android:text="main"/>
</LinearLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:0)
问题是您没有将适配器设置为RecyclerView。我建议将适配器声明移动到onCreate()
方法。并在初始化适配器后设置适配器:
mbloglist.setHasFixedSize(true);
mbloglist.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter<blog,blogviewholder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<blog, blogviewholder>(
blog.class,R.layout.blog_row,blogviewholder.class,mDatabase
) {
@Override
protected void populateViewHolder(blogviewholder viewHolder, blog model, int position) {
viewHolder.settitle(model.getPosttitle());
viewHolder.setemail(model.getPostemail());
viewHolder.settmain(model.getPostmain());
}
};
mbloglist.setAdapter(firebaseRecyclerAdapter);