将片段中的Firebase信息检索到recyclerview中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将片段中的Firebase信息检索到recyclerview中相关的知识,希望对你有一定的参考价值。
我想从我的firebase数据库中检索信息到一个名为fragment_schedule.xml
的片段中的recyclerview,每个条目的布局都在blog_row.xml
中,以下是我的代码示例;
fragment_schedule.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScheduleFragment"
android:orientation="vertical"
android:background="@drawable/blackboard2">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myrecycleView">
</android.support.v7.widget.RecyclerView>
blog_row.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
app:cardCornerRadius="12dp"
android:elevation="90dp"
android:layout_margin="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_date"
android:text="Date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_time"
android:text="Time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_event"
android:text="Event"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_moderator"
android:text="Moderator"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_venue"
android:text="Venue"/>
</LinearLayout>
</android.support.v7.widget.CardView>
schedule fragment.Java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class ScheduleFragment extends Fragment {
private RecyclerView mBlogList;
private DatabaseReference mDatabase;
public ScheduleFragment() {
// Required empty public constructor
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Schedule,ScheduleViewHolder>firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Schedule, ScheduleViewHolder>
(Schedule.class,R.layout.blog_row,ScheduleViewHolder.class, mDatabase) {
@Override
protected void populateViewHolder(ScheduleViewHolder viewHolder, Schedule
model, int position) {
viewHolder.setEvent_name( model.getEvent_name() );
viewHolder.setEvent_date( model.getEvent_date() );
viewHolder.setEvent_moderator( model.getEvent_moderator() );
viewHolder.setEvent_time( model.getEvent_time() );
viewHolder.setEvent_venue( model.getEvent_venue() );
}
};
mBlogList.setAdapter( firebaseRecyclerAdapter );
}
public static class ScheduleViewHolder extends RecyclerView.ViewHolder{
View mView;
public ScheduleViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setEvent_name(String event_name){
TextView post_event = (TextView)mView.findViewById( R.id.post_event );
post_event.setText( event_name );
}
public void setEvent_moderator(String event_moderator){
TextView post_moderator = (TextView)mView.findViewById( R.id.post_moderator );
post_moderator.setText( event_moderator );
}
public void setEvent_time(String event_time){
TextView post_time = (TextView)mView.findViewById( R.id.post_time );
post_time.setText( event_time );
}
public void setEvent_venue(String event_venue){
TextView post_venue = (TextView)mView.findViewById( R.id.post_venue );
post_venue.setText( event_venue );
}
public void setEvent_date(String event_date) {
TextView post_date = (TextView)mView.findViewById( R.id.post_date );
post_date.setText( event_date );
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("schedule");
mDatabase.keepSynced(true);
mBlogList = (RecyclerView) view.findViewById(R.id.myrecycleView);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
schedule.Java
public class Schedule {
private String event_name;
private String event_date;
private String event_moderator;
private String event_time;
private String event_venue;
public Schedule(String event_name, String event_date, String event_moderator, String
event_time, String event_venue) {
this.event_name = event_name;
this.event_date = event_date;
this.event_moderator = event_moderator;
this.event_time = event_time;
this.event_venue = event_venue;
}
public String getEvent_name() {
return event_name;
}
public void setEvent_name(String event_name) {
this.event_name = event_name;
}
public String getEvent_date() {
return event_date;
}
public void setEvent_date(String event_date) {
this.event_date = event_date;
}
public String getEvent_moderator() {
return event_moderator;
}
public void setEvent_moderator(String event_moderator) {
this.event_moderator = event_moderator;
}
public String getEvent_time() {
return event_time;
}
public void setEvent_time(String event_time) {
this.event_time = event_time;
}
public String getEvent_venue() {
return event_venue;
}
public void setEvent_venue(String event_venue) {
this.event_venue = event_venue;
}
public Schedule()
{
}
}
一切似乎都很好,应用程序运行但片段是空白的。我一次又一次地检查代码,我无法找到可能出错的地方。请帮忙
答案
您使用的是旧版本的FirebaseUI,您需要删除mBlogList.setHasFixedSize(true);
,以便数据显示在页面中。
这是旧版本中的一个错误,请点击此处:
https://github.com/firebase/FirebaseUI-Android/issues/204
以上是关于将片段中的Firebase信息检索到recyclerview中的主要内容,如果未能解决你的问题,请参考以下文章
无法从firebase数据库android,recycler视图中检索数据
将图像从 firebase 检索到 StaggeredGridLayout Android