我想从我的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>
ScheduleFragment.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()
{
}
}
一切似乎都很好,应用程序可以运行,但是片段为空。我一次又一次地检查了代码,找不到我可能出错的地方。请帮助
答案 0 :(得分:0)
您使用的是旧版本的FirebaseUI,需要删除mBlogList.setHasFixedSize(true);
,以便数据将显示在页面中。
这是旧版本中的错误,请在此处查看: